Commits

Anonymous committed 9a4ed7e

Make spin rwlock as simple as we can (do not over design)

  • Participants
  • Parent commits e5fed86

Comments (0)

Files changed (4)

include/pthread.h

 typedef long pthread_once_t;
 typedef long pthread_key_t;
 
-typedef struct {
-    long owner;
-    long ticket;
-} pthread_spinlock_t;
-
 typedef void    *pthread_mutexattr_t;
 typedef void    *pthread_condattr_t;
 typedef void    *pthread_rwlockattr_t;
 typedef void    *pthread_cond_t;
 typedef void    *pthread_rwlock_t;
 typedef void    *pthread_barrier_t;
-typedef void    *pthread_spin_rwlock_t;
+
+typedef struct {
+    long owner;
+    long ticket;
+} pthread_spinlock_t;
+
+typedef struct {
+    long owner;
+    long ticket;
+    long readers;
+} pthread_spin_rwlock_t;
 
 /*
     #include <signal.h>
     char rwlock[8]; /* InitializeSRWLock */
 } arch_rwlock;
 
-typedef struct {
-    long ticket;
-    long owner;
-    long readers;
-} arch_spin_rwlock;
-
 /** @} */
 
 #endif

src/spin_rwlock.c

 /**
  * Initialize a spin rwlock.
  * @param  lock The spin rwlock object.
- * @return If it can acquire memory immediately, the return value is 0.
- *         Otherwise, ENOMEM returned to indicate the error.
+ * @return Always return 0.
  */
 int pthread_spin_rwlock_init(pthread_spin_rwlock_t *lock)
 {
-    arch_spin_rwlock *pv = calloc(1, sizeof(arch_spin_rwlock));
-    if (pv == NULL)
-        return ENOMEM;
-
-    *lock = pv;
+    lock->owner = 0;
+    lock->ticket = 0;
+    lock->readers = 0;
 
     return 0;
 }
  */
 int pthread_spin_rwlock_reader_lock(pthread_spin_rwlock_t *lock)
 {
-    int id;
-    arch_spin_rwlock *pv = (arch_spin_rwlock *) *lock;
-    if (pv == NULL)
-        return EINVAL;
-
-    id = atomic_fetch_and_add(& pv->ticket, 1);
-    while (atomic_read(& pv->owner) != id)
+    int id = atomic_fetch_and_add(& lock->ticket, 1);
+    while (atomic_read(& lock->owner) != id)
         cpu_relax();
 
-    atomic_fetch_and_add(& pv->readers, 1);
-    pv->owner++;
+    atomic_fetch_and_add(& lock->readers, 1);
+    lock->owner++;
 
     return 0;
 }
  */
 int pthread_spin_rwlock_reader_unlock(pthread_spin_rwlock_t *lock)
 {
-    arch_spin_rwlock *pv = (arch_spin_rwlock *) *lock;
-    if (pv == NULL)
-        return EINVAL;
-
-    atomic_fetch_and_add(& pv->readers, -1);
+    atomic_fetch_and_add(& lock->readers, -1);
 
     return 0;
 }
  */
 int pthread_spin_rwlock_writer_lock(pthread_spin_rwlock_t *lock)
 {
-    int id;
-    arch_spin_rwlock *pv = (arch_spin_rwlock *) *lock;
-    if (pv == NULL)
-        return EINVAL;
-
-    id = atomic_fetch_and_add(& pv->ticket, 1);
-    while (atomic_read(& pv->owner) != id)
+    int id = atomic_fetch_and_add(& lock->ticket, 1);
+    while (atomic_read(& lock->owner) != id)
         cpu_relax();
 
-    while (atomic_read(& pv->readers) > 0)
+    while (atomic_read(& lock->readers) > 0)
         cpu_relax();
 
     return 0;
  */
 int pthread_spin_rwlock_writer_unlock(pthread_spin_rwlock_t *lock)
 {
-    arch_spin_rwlock *pv = (arch_spin_rwlock *) *lock;
-    if (pv == NULL)
-        return EINVAL;
-
-    pv->owner++;
+    lock->owner++;
 
     return 0;
 }
  */
 int pthread_spin_rwlock_destroy(pthread_spin_rwlock_t *lock)
 {
-    arch_spin_rwlock *pv = (arch_spin_rwlock *) *lock;
-    if (pv != NULL)
-        free(pv);
+    lock->owner = 0;
+    lock->ticket = 0;
+    lock->readers = 0;
 
     return 0;
 }

test/CMakeLists.txt

+ADD_EXECUTABLE (test_init test_init.c)
+ADD_EXECUTABLE (test_int64 test_int64.c)
 ADD_EXECUTABLE (test_max_key test_max_key.c)
 ADD_EXECUTABLE (test_pause test_pause.c)
 ADD_EXECUTABLE (test_realtime test_realtime.c)
 ADD_EXECUTABLE (test_size test_size.c)
 ADD_EXECUTABLE (test_sleep test_sleep.c)
 
-ADD_EXECUTABLE (test_sched test_sched.c)
-TARGET_LINK_LIBRARIES (test_sched ${LIBPTHREAD_NAME})
-
-ADD_EXECUTABLE (test_sem test_sem.c)
-TARGET_LINK_LIBRARIES (test_sem ${LIBPTHREAD_NAME})
-
 ADD_EXECUTABLE (test_clock test_clock.c)
 TARGET_LINK_LIBRARIES (test_clock ${LIBPTHREAD_NAME})
 
-ADD_EXECUTABLE (test_thread_create test_thread_create.c)
-TARGET_LINK_LIBRARIES (test_thread_create ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_clock_getres test_clock_getres.c)
+TARGET_LINK_LIBRARIES (test_clock_getres ${LIBPTHREAD_NAME})
 
-ADD_EXECUTABLE (test_thread_join test_thread_join.c)
-TARGET_LINK_LIBRARIES (test_thread_join ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_clock_gettime test_clock_gettime.c)
+TARGET_LINK_LIBRARIES (test_clock_gettime ${LIBPTHREAD_NAME})
+
+ADD_EXECUTABLE (test_clock_nanosleep test_clock_nanosleep.c)
+TARGET_LINK_LIBRARIES (test_clock_nanosleep ${LIBPTHREAD_NAME})
+
+ADD_EXECUTABLE (test_clock_settime test_clock_settime.c)
+TARGET_LINK_LIBRARIES (test_clock_settime ${LIBPTHREAD_NAME})
 
 ADD_EXECUTABLE (test_key test_key.c)
 TARGET_LINK_LIBRARIES (test_key ${LIBPTHREAD_NAME})
 ADD_EXECUTABLE (test_mutex test_mutex.c)
 TARGET_LINK_LIBRARIES (test_mutex ${LIBPTHREAD_NAME})
 
+ADD_EXECUTABLE (test_nanosleep test_nanosleep.c)
+TARGET_LINK_LIBRARIES (test_nanosleep ${LIBPTHREAD_NAME})
+
 ADD_EXECUTABLE (test_once test_once.c)
 TARGET_LINK_LIBRARIES (test_once ${LIBPTHREAD_NAME})
 
-ADD_EXECUTABLE (test_spin test_spin.c)
-TARGET_LINK_LIBRARIES (test_spin ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_sched test_sched.c)
+TARGET_LINK_LIBRARIES (test_sched ${LIBPTHREAD_NAME})
+
+ADD_EXECUTABLE (test_sem test_sem.c)
+TARGET_LINK_LIBRARIES (test_sem ${LIBPTHREAD_NAME})
 
 ADD_EXECUTABLE (test_speed test_speed.c)
 TARGET_LINK_LIBRARIES (test_speed ${LIBPTHREAD_NAME})
 
-ADD_EXECUTABLE (test_nanosleep test_nanosleep.c)
-TARGET_LINK_LIBRARIES (test_nanosleep ${LIBPTHREAD_NAME})
-
-ADD_EXECUTABLE (test_clock_getres test_clock_getres.c)
-TARGET_LINK_LIBRARIES (test_clock_getres ${LIBPTHREAD_NAME})
-
-ADD_EXECUTABLE (test_clock_gettime test_clock_gettime.c)
-TARGET_LINK_LIBRARIES (test_clock_gettime ${LIBPTHREAD_NAME})
-
-ADD_EXECUTABLE (test_clock_nanosleep test_clock_nanosleep.c)
-TARGET_LINK_LIBRARIES (test_clock_nanosleep ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_thread_create test_thread_create.c)
+TARGET_LINK_LIBRARIES (test_thread_create ${LIBPTHREAD_NAME})
 
-ADD_EXECUTABLE (test_clock_settime test_clock_settime.c)
-TARGET_LINK_LIBRARIES (test_clock_settime ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_thread_join test_thread_join.c)
+TARGET_LINK_LIBRARIES (test_thread_join ${LIBPTHREAD_NAME})
+ADD_EXECUTABLE (test_spin test_spin.c)
+TARGET_LINK_LIBRARIES (test_spin ${LIBPTHREAD_NAME})
 
 # http://www.cmake.org/Wiki/CMake_Testing_With_CTest
+#ADD_TEST (test_init test_init)
+#ADD_TEST (test_int64 test_int64)
 #ADD_TEST (test_max_key test_max_key)
 #ADD_TEST (test_pause test_pause)
 #ADD_TEST (test_realtime test_realtime)