File: myth_felock.c

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (60 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download
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
/* 
 * myth_felock.c --- test full empty 
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include <myth/myth.h>

myth_felock_t fe[1];
long count;

typedef struct {
  myth_thread_t tid;
  long n;
} arg_t;

void * incrementer(void * arg_) {
  arg_t * arg = (arg_t *)arg_;
  long n = arg->n;
  long i;
  for (i = 0; i < n; i++) {
    /* wait until full and lock it */
    myth_felock_wait_and_lock(fe, 1);
    long c = count;
    /* mark it's empty */
    myth_felock_mark_and_signal(fe, 0);
    /* wait until empty and lock it */
    myth_felock_wait_and_lock(fe, 0);
    count = c + 1;
    /* mark it's full */
    myth_felock_mark_and_signal(fe, 1);
  }
  return 0;
}

int main(int argc, char ** argv) {
  long n_threads = (argc > 1 ? atol(argv[1]) : 100);
  long n = (argc > 2 ? atol(argv[2]) : 1000);
  long i;
  arg_t arg = { 0, n };
  arg_t * args = (arg_t *)calloc(sizeof(arg_t), n_threads);

  myth_felock_init(fe, 0);
  for (i = 0; i < n_threads; i++) {
    args[i] = arg;
    args[i].tid = myth_create(incrementer, &args[i]);
  }
  const long init_count = 123;
  count = init_count;
  myth_felock_wait_and_lock(fe, 0);
  myth_felock_mark_and_signal(fe, 1);
  for (i = 0; i < n_threads; i++) {
    myth_join(args[i].tid, 0);
  }
  assert(count == n_threads * n + init_count);
  printf("OK\n");
  return 0;
}