File: mutex1.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (55 lines) | stat: -rw-r--r-- 1,539 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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "dmtcp.h"

// This code uses dmtcp_mutex_trylock() and dmtcp_mutex_unlock().
// We could alternatively have used pthread_mutex_locak/unlock,
//   but in this case, it will simply hang on restart, and our
//   current autotest can't check for processes that live, but hang.
// This code tests on a recursive mutex, since a recursive mutex
//   must check the owner when locking.

int main() {
  // DMTCP and native differ when this is not initialized.  Why?
  // DMTCP crashed on lock with ESRCH
  int counter = 0;

  pthread_mutex_t mutex;
  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  pthread_mutex_init(&mutex, &attr);

  dmtcp_disable_ckpt();
  while (1) {
    int rc;
    rc = pthread_mutex_trylock(&mutex);
    if (rc != 0) {
      printf("pthread_mutex_trylock (recursive): %s\nn",
             strerror(rc));
      exit(1);
    }
    dmtcp_enable_ckpt();
    if (counter++ % 1000000 == 0) {
      printf("b"); fflush(stdout);
    }
    dmtcp_disable_ckpt();
    rc = pthread_mutex_trylock(&mutex);
    if (rc != 0) {
      printf("pthread_mutex_trylock (recursive): %s\n\n",
             strerror(rc));
      exit(1);
    }
    rc = pthread_mutex_unlock(&mutex);
    rc = pthread_mutex_unlock(&mutex);
    if (rc != 0) {
      printf("pthread_mutex_unlock (recursive): %s\n\n",
             strerror(rc));
      exit(1);
    }
  }
  return 0;
}