Compare commits
2 Commits
fe2590240b
...
aaa595dbb7
Author | SHA1 | Date |
---|---|---|
Vftdan | aaa595dbb7 | |
Vftdan | 8dd0ffdb3b |
|
@ -0,0 +1,35 @@
|
|||
#ifndef COMMON_UTIL_SPIN_LOCK_H_
|
||||
#define COMMON_UTIL_SPIN_LOCK_H_
|
||||
|
||||
#include "common/util/thread.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <errno.h>
|
||||
|
||||
typedef struct {
|
||||
volatile atomic_flag flag;
|
||||
} SpinLock;
|
||||
|
||||
HEADER_FN bool
|
||||
spin_lock_try_acquire(SpinLock *lock)
|
||||
{
|
||||
return !atomic_flag_test_and_set(&lock->flag);
|
||||
}
|
||||
|
||||
HEADER_FN void
|
||||
spin_lock_acquire(SpinLock *lock)
|
||||
{
|
||||
int save_errno = errno;
|
||||
while (!spin_lock_try_acquire(lock)) {
|
||||
thread_yield();
|
||||
}
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
HEADER_FN void
|
||||
spin_lock_release(SpinLock *lock)
|
||||
{
|
||||
atomic_flag_clear(&lock->flag);
|
||||
}
|
||||
|
||||
#endif /* end of include guard: COMMON_UTIL_SPIN_LOCK_H_ */
|
|
@ -37,8 +37,20 @@ Thread thread_delete_handle(Thread th);
|
|||
* Waits for a thread to exit
|
||||
* @param th the thread to join
|
||||
* @param result_ptr pointer to store the exit result or NULL
|
||||
* @return bool on success, false on failure (errno may be set)
|
||||
* @return true on success, false on failure (errno may be set)
|
||||
*/
|
||||
bool thread_join(Thread th, ThreadResult *result_ptr);
|
||||
|
||||
/**
|
||||
* Terminates current thread
|
||||
* @param result exit result to return via thread_join
|
||||
*/
|
||||
void thread_exit(ThreadResult result) __attribute__((noreturn));
|
||||
|
||||
/**
|
||||
* Hints the OS to release CPU used by this thread
|
||||
* @return true on success, false on failure (errno may be set)
|
||||
*/
|
||||
bool thread_yield();
|
||||
|
||||
#endif /* end of include guard: COMMON_UTIL_THREAD_H_ */
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "thread.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
@ -114,3 +115,15 @@ thread_join(Thread th, ThreadResult *result_ptr)
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
thread_exit(ThreadResult result)
|
||||
{
|
||||
pthread_exit(result.pointer);
|
||||
}
|
||||
|
||||
bool
|
||||
thread_yield()
|
||||
{
|
||||
return !sched_yield();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "common/util/hash_table.h"
|
||||
#include "common/util/byte_serdes.h"
|
||||
#include "common/util/thread.h"
|
||||
#include "common/util/spin_lock.h"
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
|
|
Loading…
Reference in New Issue