aboutsummaryrefslogtreecommitdiff
path: root/tests/threads/test1.c
blob: 8d6cb6747ddfa7024d1b9f904cf07cb56325398e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include "uthash.h"

#undef uthash_noexpand_fyi
#define uthash_noexpand_fyi(tbl) fprintf(stderr,"warning: bucket expansion inhibited\n")

#define LOOPS 100000

typedef struct {
  int i;
  UT_hash_handle hh;
} elt;

elt *elts=NULL; /* this is our hash table which two threads will use */
pthread_rwlock_t lock;

void *thread_routine_r( void *arg ) {
    int i;
    long num_found=0;
    elt *e;

    for(i=0;i<LOOPS;i++) {
      if (pthread_rwlock_rdlock(&lock) != 0) {
        fprintf(stderr,"can't acquire read lock\n");
        exit(-1);
      }
      HASH_FIND_INT(elts, &i, e);
      if (e) num_found++;
      pthread_rwlock_unlock(&lock);
    }
    return (void*)num_found;
}
void *thread_routine_w( void *arg ) {
    int i;
    long num_deld=0;
    elt *e;

    for(i=0;i<LOOPS;i++) {
      if (pthread_rwlock_wrlock(&lock) != 0) {
        fprintf(stderr,"can't acquire write lock\n");
        exit(-1);
      }
      HASH_FIND_INT(elts, &i, e);
      if (e) {
        /* HASH_DEL(elts, e); */
        /* num_deld++; */
      } else {
        e = malloc(sizeof(elt));
        if (!e) exit(-1);
        e->i = i;
        HASH_ADD_INT(elts, i, e);
      }
      pthread_rwlock_unlock(&lock);
    }
    return (void*)num_deld;
}

int main() {
    unsigned i;
    long num_added=0;
    int status;
    pthread_t thread_r1,thread_r2,thread_w1,thread_w2;
    void *thread_result;
    elt tmp, *e;

    if (pthread_rwlock_init(&lock,NULL) != 0) {
      fprintf(stderr,"lock init failed\n");
      exit(-1);
    }

    if (( status = pthread_create( &thread_r1, NULL, thread_routine_r, NULL) )) {
        printf("failure: status %d\n", status);
        exit(-1);
    }
    if (( status = pthread_create( &thread_r2, NULL, thread_routine_r, NULL) )) {
        printf("failure: status %d\n", status);
        exit(-1);
    }
    if (( status = pthread_create( &thread_w1, NULL, thread_routine_w, NULL) )) {
        printf("failure: status %d\n", status);
        exit(-1);
    }
    if (( status = pthread_create( &thread_w2, NULL, thread_routine_w, NULL) )) {
        printf("failure: status %d\n", status);
        exit(-1);
    }

    status = pthread_join( thread_r1, &thread_result );
    printf("thread result: %d %ld\n", status, (long)thread_result);

    status = pthread_join( thread_r2, &thread_result );
    printf("thread result: %d %ld\n", status, (long)thread_result);

    status = pthread_join( thread_w1,  &thread_result );
    printf("thread result: %d %ld\n", status, (long)thread_result);

    status = pthread_join( thread_w2,  &thread_result );
    printf("thread result: %d %ld\n", status, (long)thread_result);

    i = HASH_COUNT(elts);
    printf("final count of items in hash: %u\n", i);

    if (pthread_rwlock_destroy(&lock) != 0) {
      fprintf(stderr,"lock destroy failed\n");
      exit(-1);
    }
}