Skip to content

Commit

Permalink
Add rwlock test
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 22, 2017
1 parent e8e4860 commit 086259b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
25 changes: 19 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
cmake_minimum_required (VERSION 3.8.2)
project(KeyValueStorage)

add_compile_options(-std=c11)
include_directories(include)

file(GLOB SOURCES "src/*.c")
file(GLOB sources "src/*.c")
list(REMOVE_ITEM sources "src/node.c" "src/hashtable.c" "src/thpool.c")

add_library(node "src/node.c")
add_library(hashtable "src/hashtable.c")

add_library(thpool "src/thpool.c")

add_executable(main ${sources})
add_executable(node_test "test/node_test.c")
add_executable(hash_test "test/hash_test.c")
add_executable(thpool_rwlock_test "test/thpool_rwlock_test.c")

add_executable(main ${SOURCES})
add_executable(node_test "test/node_test.c" "src/node.c")
add_executable(hash_test "test/hash_test.c" "src/node.c" "src/hashtable.c")
target_link_libraries(main pthread)
target_link_libraries(hashtable node)
target_link_libraries(thpool pthread)
target_link_libraries(main hashtable thpool)
target_link_libraries(node_test node)
target_link_libraries(hash_test hashtable)
target_link_libraries(thpool_rwlock_test thpool)
46 changes: 46 additions & 0 deletions test/thpool_rwlock_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include "thpool.h"

typedef threadpool threadpool_t;

static pthread_rwlock_t rwlock;
static threadpool_t thpool;

void read_task() {
pthread_rwlock_rdlock(&rwlock);
int i;
for (i = 0; i < 20; i++) {
printf("read_task_%d in %u\n", i, (int) pthread_self());
}
pthread_rwlock_unlock(&rwlock);
}

void write_task() {
pthread_rwlock_wrlock(&rwlock);
int i;
for (i = 0; i < 20; i++) {
printf("write_task_%d in %u\n", i, (int) pthread_self());
}
pthread_rwlock_unlock(&rwlock);
}

int main() {
thpool = thpool_init(4);
pthread_rwlock_init(&rwlock, NULL);

int i;

for (i = 0; i < 4; i++) {
if (i == 2) {
thpool_add_work(thpool, (void *) write_task, NULL);
}
thpool_add_work(thpool, (void *) read_task, NULL);
}

sleep(1);
thpool_destroy(thpool);
pthread_rwlock_destroy(&rwlock);
return 0;
}

0 comments on commit 086259b

Please sign in to comment.