-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72886f2
commit e63515b
Showing
9 changed files
with
345 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Swoole | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 2.0 of the Apache license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| http://www.apache.org/licenses/LICENSE-2.0.html | | ||
| If you did not receive a copy of the Apache2.0 license and are unable| | ||
| to obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Author: NathanFreeman <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#include "swoole.h" | ||
|
||
#ifdef HAVE_IOURING_FUTEX | ||
#include "swoole_iouring.h" | ||
#else | ||
#include "swoole_coroutine_system.h" | ||
using swoole::coroutine::System; | ||
#endif | ||
|
||
#include "swoole_lock.h" | ||
|
||
namespace swoole { | ||
CoroutineLock::CoroutineLock() : Lock() { | ||
type_ = COROUTINE_LOCK; | ||
value = (sw_atomic_t *) sw_mem_pool()->alloc(sizeof(sw_atomic_t)); | ||
*value = 0; | ||
} | ||
|
||
CoroutineLock::~CoroutineLock() { | ||
sw_mem_pool()->free((void *) value); | ||
value = nullptr; | ||
} | ||
|
||
int CoroutineLock::lock() { | ||
return lock_impl(true); | ||
} | ||
|
||
int CoroutineLock::trylock() { | ||
return lock_impl(false); | ||
} | ||
|
||
int CoroutineLock::lock_rd() { | ||
return lock_impl(true); | ||
} | ||
|
||
int CoroutineLock::trylock_rd() { | ||
return lock_impl(false); | ||
} | ||
|
||
int CoroutineLock::unlock() { | ||
Coroutine *current_coroutine = Coroutine::get_current(); | ||
if (current_coroutine == nullptr) { | ||
swoole_warning("The coroutine lock can only be used in a coroutine environment"); | ||
return 1; | ||
} | ||
|
||
if (*value == 0) { | ||
return 0; | ||
} | ||
|
||
*value = 0; | ||
cid = 0; | ||
coroutine = nullptr; | ||
#ifdef HAVE_IOURING_FUTEX | ||
return Iouring::wakeup_futex((uint32_t *) value) >= 0 ? 0 : 1; | ||
#else | ||
return 0; | ||
#endif | ||
} | ||
|
||
int CoroutineLock::lock_impl(bool blocking) { | ||
Coroutine *current_coroutine = Coroutine::get_current(); | ||
if (current_coroutine == nullptr) { | ||
swoole_warning("The coroutine lock can only be used in a coroutine environment"); | ||
return 1; | ||
} | ||
|
||
if (current_coroutine == static_cast<Coroutine *>(coroutine) && current_coroutine->get_cid() == cid) { | ||
return 0; | ||
} | ||
|
||
int result = 0; | ||
#ifdef HAVE_IOURING_FUTEX | ||
if (!sw_atomic_cmp_set(value, 0, 1)) { | ||
if (!blocking) { | ||
return 1; | ||
} | ||
|
||
result = Iouring::wait_futex((uint32_t *) value); | ||
*value = 1; | ||
} | ||
#else | ||
while (true) { | ||
if (sw_atomic_cmp_set(value, 0, 1)) { | ||
break; | ||
} | ||
|
||
if (!blocking) { | ||
return 1; | ||
} | ||
|
||
if (System::sleep((double) 0.1) != 0) { | ||
return 1; | ||
} | ||
} | ||
#endif | ||
|
||
cid = current_coroutine->get_cid(); | ||
coroutine = (void *) current_coroutine; | ||
return result; | ||
} | ||
} // namespace swoole |
Oops, something went wrong.