mc-manual.xml fix new/delete expresions -> expression
[valgrind.git] / none / tests / pth_cancel2.c
blob63fd75a3f3bf788770bc7b766bb35e4fe0a42e3b
1 /********************************************************
2 * An example source module to accompany...
4 * "Using POSIX Threads: Programming with Pthreads"
5 * by Brad nichols, Dick Buttlar, Jackie Farrell
6 * O'Reilly & Associates, Inc.
8 ********************************************************
9 * async_safe --
11 * Example showing macro wrappers for calling non-async
12 * safe routines when the caller has asynchronous
13 * cancellation turned on
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <pthread.h>
26 #define async_cancel_safe_read(fd,buf,amt) \
27 { \
28 int oldtype; \
29 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
30 if (read(fd,buf,amt) < 0) \
31 perror("read"),exit(1); \
32 pthread_setcanceltype(oldtype,NULL); \
33 pthread_testcancel(); \
37 #define async_cancel_safe_write(fd,buf,amt) \
38 { \
39 int oldtype; \
40 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
41 if (write(fd,buf,amt) < 0) \
42 perror("write"), exit(1); \
43 pthread_setcanceltype(oldtype,NULL); \
44 pthread_testcancel(); \
48 static int fd;
50 void *io(void *arg)
52 int *fd2=(int *)arg;
53 char buf[20]="String";
54 int amt=20;
56 for (;;) {
57 async_cancel_safe_write(*fd2,buf,amt);
58 async_cancel_safe_read(*fd2,buf,amt);
60 return(NULL);
63 void *killer(void *arg)
65 pthread_t * target = (pthread_t *)arg;
66 sleep(1);
67 pthread_cancel(*target);
68 return(NULL);
71 extern int
72 main(void)
74 pthread_t io_thread, killer_thread;
76 // extern void *io(void *);
77 // extern void *killer(void *);
79 if ((fd = open(".ktemp",O_CREAT | O_RDWR, 0666)) < 0)
80 perror("open"), exit(1);
82 pthread_create(&io_thread,
83 NULL,
84 io,
85 (void *)&fd);
86 pthread_create(&killer_thread,
87 NULL,
88 killer,
89 (void *)&io_thread);
91 pthread_join(io_thread, NULL);
93 pthread_join(killer_thread,NULL);
95 if ((close(fd)) < 0)
96 perror("close"),exit(1);
97 if ((unlink(".ktemp")) < 0)
98 perror("unlink"),exit(1);
100 return 0;