| 536 | } |
| 537 | |
| 538 | int bthread_id_trylock(bthread_id_t id, void** pdata) { |
| 539 | bthread::Id* const meta = address_resource(bthread::get_slot(id)); |
| 540 | if (!meta) { |
| 541 | return EINVAL; |
| 542 | } |
| 543 | uint32_t* butex = meta->butex; |
| 544 | const uint32_t id_ver = bthread::get_version(id); |
| 545 | meta->mutex.lock(); |
| 546 | if (!meta->has_version(id_ver)) { |
| 547 | meta->mutex.unlock(); |
| 548 | return EINVAL; |
| 549 | } |
| 550 | if (*butex != meta->first_ver) { |
| 551 | meta->mutex.unlock(); |
| 552 | return EBUSY; |
| 553 | } |
| 554 | *butex = meta->locked_ver; |
| 555 | meta->mutex.unlock(); |
| 556 | if (pdata != NULL) { |
| 557 | *pdata = meta->data; |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | int bthread_id_lock_verbose(bthread_id_t id, void** pdata, |
| 563 | const char *location) { |