Anonymous avatar Anonymous committed 0bf0e90

Add test_spin_rwlock.c

Comments (0)

Files changed (5)

include/pthread.h

 #define PTHREAD_MUTEX_ROBUST        1
 
 #define PTHREAD_SPINLOCK_INITIALIZER    {0, 0}
+#define PTHREAD_SPIN_RWLOCK_INITIALIZER {0, 0, 0}
 #define PTHREAD_MUTEX_INITIALIZER       NULL
 #define PTHREAD_RWLOCK_INITIALIZER      NULL
 #define PTHREAD_COND_INITIALIZER        NULL
 int pthread_spin_unlock(pthread_spinlock_t *lock);
 int pthread_spin_destroy(pthread_spinlock_t *lock);
 
-int pthread_spin_rwlock_init(pthread_spin_rwlock_t *lock);
+int pthread_spin_rwlock_init(pthread_spin_rwlock_t *lock, int pshared);
 int pthread_spin_rwlock_reader_lock(pthread_spin_rwlock_t *lock);
 int pthread_spin_rwlock_reader_unlock(pthread_spin_rwlock_t *lock);
 int pthread_spin_rwlock_writer_lock(pthread_spin_rwlock_t *lock);
 
     lock->owner = 0;
     lock->ticket = 0;
+
     return 0;
 }
 
 int pthread_spin_unlock(pthread_spinlock_t *lock)
 {
     lock->owner++;
+
     return 0;
 }
 
 {
     lock->owner = 0;
     lock->ticket = 0;
+
     return 0;
 }

src/spin_rwlock.c

 /**
  * Initialize a spin rwlock.
  * @param  lock The spin rwlock object.
- * @return Always return 0.
+ * @param  pshared Must be PTHREAD_PROCESS_PRIVATE (0).
+ * @return If the pshared is PTHREAD_PROCESS_PRIVATE, the return value is 0.
+ *         Otherwise, EINVAL returned to indicate the error.
  */
-int pthread_spin_rwlock_init(pthread_spin_rwlock_t *lock)
+int pthread_spin_rwlock_init(pthread_spin_rwlock_t *lock, int pshared)
 {
+    if (PTHREAD_PROCESS_PRIVATE != pshared)
+        return EINVAL;
+
     lock->owner = 0;
     lock->ticket = 0;
     lock->readers = 0;

test/CMakeLists.txt

 ADD_EXECUTABLE (test_speed test_speed.c)
 TARGET_LINK_LIBRARIES (test_speed ${LIBPTHREAD_NAME})
 
+ADD_EXECUTABLE (test_spin test_spin.c)
+TARGET_LINK_LIBRARIES (test_spin ${LIBPTHREAD_NAME})
+
+ADD_EXECUTABLE (test_spin_rwlock test_spin_rwlock.c)
+TARGET_LINK_LIBRARIES (test_spin_rwlock ${LIBPTHREAD_NAME})
+
 ADD_EXECUTABLE (test_thread_create test_thread_create.c)
 TARGET_LINK_LIBRARIES (test_thread_create ${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_sem test_sem)
 #ADD_TEST (test_speed test_speed)
 ADD_TEST (test_spin test_spin)
+ADD_TEST (test_spin_rwlock test_spin_rwlock)
 ADD_TEST (test_thread_create test_thread_create)
 ADD_TEST (test_thread_join test_thread_join)

test/test_spin_rwlock.c

+/*
+ * Copyright (c) 2011, Dongsheng Song <songdongsheng@live.cn>
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <winsock2.h>
+#include <pthread.h>
+
+#include "../src/misc.h"
+
+pthread_spin_rwlock_t lock = PTHREAD_SPIN_RWLOCK_INITIALIZER;
+
+int main(int argc, char *argv[])
+{
+    assert(pthread_spin_rwlock_init(&lock, PTHREAD_PROCESS_PRIVATE) == 0);
+    printf("pthread_spin_rwlock_init passed\n");
+
+    assert(pthread_spin_rwlock_writer_lock(&lock) == 0);
+    printf("pthread_spin_rwlock_writer_lock passed\n");
+
+    assert(pthread_spin_rwlock_writer_unlock(&lock) == 0);
+    printf("pthread_spin_rwlock_writer_unlock passed\n");
+
+    assert(pthread_spin_rwlock_reader_lock(&lock) == 0);
+    assert(pthread_spin_rwlock_reader_lock(&lock) == 0);
+    assert(pthread_spin_rwlock_reader_lock(&lock) == 0);
+    printf("pthread_spin_rwlock_reader_lock passed\n");
+
+    assert(pthread_spin_rwlock_reader_unlock(&lock) == 0);
+    assert(pthread_spin_rwlock_reader_unlock(&lock) == 0);
+    assert(pthread_spin_rwlock_reader_unlock(&lock) == 0);
+    printf("pthread_spin_rwlock_reader_unlock passed\n");
+
+    assert(pthread_spin_rwlock_destroy(&lock) == 0);
+    printf("pthread_spin_rwlock_destroy passed\n");
+
+    return 0;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.