* @brief Sends a cancellation request to a specified thread. * * The `pthread_cancel` function requests the cancellation of the thread identified by `thread`. * The actual response to the request depends on the target thread's cancelability state and type. * * @param[in] thread * The identifier of the thread to be canceled. * * @return * - `0` on success. * - `EINVAL` if the specif
| 1440 | * @see pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel |
| 1441 | */ |
| 1442 | int pthread_cancel(pthread_t thread) |
| 1443 | { |
| 1444 | _pthread_data_t *ptd; |
| 1445 | _pthread_cleanup_t *cleanup; |
| 1446 | rt_thread_t tid; |
| 1447 | |
| 1448 | /* get posix thread data */ |
| 1449 | ptd = _pthread_get_data(thread); |
| 1450 | if (ptd == RT_NULL) |
| 1451 | { |
| 1452 | return EINVAL; |
| 1453 | } |
| 1454 | tid = ptd->tid; |
| 1455 | |
| 1456 | /* cancel self */ |
| 1457 | if (ptd->tid == rt_thread_self()) |
| 1458 | return 0; |
| 1459 | |
| 1460 | /* set canceled */ |
| 1461 | if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE) |
| 1462 | { |
| 1463 | ptd->canceled = 1; |
| 1464 | if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 1465 | { |
| 1466 | /* |
| 1467 | * When use pthread_cancel to exit. |
| 1468 | * invoke pushed cleanup |
| 1469 | */ |
| 1470 | while (ptd->cleanup != RT_NULL) |
| 1471 | { |
| 1472 | cleanup = ptd->cleanup; |
| 1473 | ptd->cleanup = cleanup->next; |
| 1474 | |
| 1475 | cleanup->cleanup_func(cleanup->parameter); |
| 1476 | /* release this cleanup function */ |
| 1477 | rt_free(cleanup); |
| 1478 | } |
| 1479 | |
| 1480 | /* According to "detachstate" to whether or not to recycle resource immediately */ |
| 1481 | if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE) |
| 1482 | { |
| 1483 | /* set value */ |
| 1484 | rt_sem_release(ptd->joinable_sem); |
| 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | /* release pthread resource */ |
| 1489 | _pthread_data_destroy(ptd); |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * second: detach thread. |
| 1494 | * this thread will be removed from scheduler list |
| 1495 | * and because there is a cleanup function in the |
| 1496 | * thread (pthread_cleanup), it will move to defunct |
| 1497 | * thread list and wait for handling in idle thread. |
| 1498 | */ |
| 1499 | rt_thread_detach(tid); |
no test coverage detected