-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[threading] Switch to a native implementation of LowLevelLifoSemaphore #2098
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <mono/utils/lifo-semaphore.h> | ||
|
||
LifoSemaphore * | ||
mono_lifo_semaphore_init (void) | ||
{ | ||
LifoSemaphore *semaphore = g_new0 (LifoSemaphore, 1); | ||
if (semaphore == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you have to use |
||
return NULL; | ||
|
||
mono_coop_mutex_init (&semaphore->mutex); | ||
|
||
return semaphore; | ||
} | ||
|
||
void | ||
mono_lifo_semaphore_delete (LifoSemaphore *semaphore) | ||
{ | ||
g_assert (semaphore->head == NULL); | ||
mono_coop_mutex_destroy (&semaphore->mutex); | ||
g_free (semaphore); | ||
} | ||
|
||
int32_t | ||
mono_lifo_semaphore_timed_wait (LifoSemaphore *semaphore, int32_t timeout_ms) | ||
{ | ||
LifoSemaphoreWaitEntry wait_entry = { 0 }; | ||
|
||
mono_coop_cond_init (&wait_entry.condition); | ||
mono_coop_mutex_lock (&semaphore->mutex); | ||
|
||
if (semaphore->pending_signals > 0) { | ||
--semaphore->pending_signals; | ||
mono_coop_cond_destroy (&wait_entry.condition); | ||
mono_coop_mutex_unlock (&semaphore->mutex); | ||
return 1; | ||
} | ||
|
||
// Enqueue out entry into the LIFO wait list | ||
wait_entry.previous = NULL; | ||
wait_entry.next = semaphore->head; | ||
if (semaphore->head != NULL) | ||
semaphore->head->previous = &wait_entry; | ||
semaphore->head = &wait_entry; | ||
|
||
// Wait for a signal or timeout | ||
int wait_error = 0; | ||
do { | ||
wait_error = mono_coop_cond_timedwait (&wait_entry.condition, &semaphore->mutex, timeout_ms); | ||
} while (wait_error == 0 && !wait_entry.signaled); | ||
|
||
if (wait_error == -1) { | ||
if (semaphore->head == &wait_entry) | ||
semaphore->head = wait_entry.next; | ||
if (wait_entry.next != NULL) | ||
wait_entry.next->previous = wait_entry.previous; | ||
if (wait_entry.previous != NULL) | ||
wait_entry.previous->next = wait_entry.next; | ||
} | ||
|
||
mono_coop_cond_destroy (&wait_entry.condition); | ||
mono_coop_mutex_unlock (&semaphore->mutex); | ||
|
||
return wait_entry.signaled; | ||
} | ||
|
||
void | ||
mono_lifo_semaphore_release (LifoSemaphore *semaphore, uint32_t count) | ||
{ | ||
mono_coop_mutex_lock (&semaphore->mutex); | ||
|
||
while (count > 0) { | ||
LifoSemaphoreWaitEntry *wait_entry = semaphore->head; | ||
if (wait_entry != NULL) { | ||
semaphore->head = wait_entry->next; | ||
if (semaphore->head != NULL) | ||
semaphore->head->previous = NULL; | ||
wait_entry->previous = NULL; | ||
wait_entry->next = NULL; | ||
wait_entry->signaled = 1; | ||
mono_coop_cond_signal (&wait_entry->condition); | ||
--count; | ||
} else { | ||
semaphore->pending_signals += count; | ||
count = 0; | ||
} | ||
} | ||
|
||
mono_coop_mutex_unlock (&semaphore->mutex); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef __MONO_LIFO_SEMAPHORE_H__ | ||
#define __MONO_LIFO_SEMAPHORE_H__ | ||
|
||
#include <mono/utils/mono-coop-mutex.h> | ||
|
||
typedef struct _LifoSemaphore LifoSemaphore; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we maybe drop the leading underscore: |
||
typedef struct _LifoSemaphoreWaitEntry LifoSemaphoreWaitEntry; | ||
|
||
struct _LifoSemaphoreWaitEntry { | ||
MonoCoopCond condition; | ||
int signaled; | ||
LifoSemaphoreWaitEntry *previous; | ||
LifoSemaphoreWaitEntry *next; | ||
}; | ||
|
||
struct _LifoSemaphore { | ||
MonoCoopMutex mutex; | ||
LifoSemaphoreWaitEntry *head; | ||
uint32_t pending_signals; | ||
}; | ||
|
||
LifoSemaphore * | ||
mono_lifo_semaphore_init (void); | ||
|
||
void | ||
mono_lifo_semaphore_delete (LifoSemaphore *semaphore); | ||
|
||
int32_t | ||
mono_lifo_semaphore_timed_wait (LifoSemaphore *semaphore, int32_t timeout_ms); | ||
|
||
void | ||
mono_lifo_semaphore_release (LifoSemaphore *semaphore, uint32_t count); | ||
|
||
#endif // __MONO_LIFO_SEMAPHORE_H__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines could be combined.
In fact, you could just declare the function to be taking
LifoSemaphore*
and remove the cast.You could also expose
mono_lifo_semaphore_delete
and save the call/jmp.