/var/lib/pcp/testsuite/src/multithread2.c is in pcp-testsuite 3.8.12ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /*
* Copyright (c) 2012-2013 Red Hat.
* Copyright (c) 2011 Ken McDonell. All Rights Reserved.
*
* exercise multi-threaded checks for PM_SCOPE_AF and PM_SCOPE_ACL
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>
#include <pthread.h>
#ifndef HAVE_PTHREAD_BARRIER_T
#include "pthread_barrier.h"
#endif
#include "localconfig.h"
#if PCP_VER >= 3611
__pmSockAddr *addr;
#else
__pmIPAddr addr;
#endif
static pthread_barrier_t barrier;
static void
wakeup(int tag, void *data)
{
printf("Woops, wakeup(%d, %p) called?\n", tag, data);
}
static void *
func1(void *arg)
{
int sts;
int afid;
struct timeval when = { 1000, 0 };
char *fn = "func1";
unsigned int op;
afid = __pmAFregister(&when, NULL, wakeup);
if (afid >= 0)
printf("%s: __pmAFregister -> OK\n", fn);
else
printf("%s: __pmAFregister -> %s\n", fn, pmErrStr(afid));
sts = __pmAFunregister(afid);
if (sts >= 0)
printf("%s: __pmAFunregister -> OK\n", fn);
else
printf("%s: __pmAFunregister -> %s\n", fn, pmErrStr(sts));
pthread_barrier_wait(&barrier);
pthread_barrier_wait(&barrier);
sts = __pmAccAddOp(1);
if (sts == 0)
printf("%s: __pmAccAddOp(1) -> OK\n", fn);
else
printf("%s: __pmAccAddOp(1) -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddOp(2);
if (sts == 0)
printf("%s: __pmAccAddOp(2) -> OK\n", fn);
else
printf("%s: __pmAccAddOp(2) -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddHost("localhost", 1, 2, 3);
if (sts == 0)
printf("%s: __pmAccAddHost -> OK\n", fn);
else
printf("%s: __pmAccAddHost -> %s\n", fn, pmErrStr(sts));
#if PCP_VER >= 3801
putc('\n', stdout);
#endif
__pmAccDumpHosts(stdout);
#if PCP_VER >= 3801
putc('\n', stdout);
#endif
sts = __pmAccSaveHosts();
if (sts == 0)
printf("%s: __pmAccSaveHosts -> OK\n", fn);
else
printf("%s: __pmAccSaveHosts -> %s\n", fn, pmErrStr(sts));
sts = __pmAccRestoreHosts();
if (sts == 0)
printf("%s: __pmAccRestoreHosts -> OK\n", fn);
else
printf("%s: __pmAccRestoreHosts -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddClient(addr, &op);
if (sts == 0)
printf("%s: __pmAccAddClient -> %d\n", fn, op);
else
printf("%s: __pmAccAddClient -> %s\n", fn, pmErrStr(sts));
pthread_barrier_wait(&barrier);
pthread_exit(NULL);
}
static void *
func2(void *arg)
{
int sts;
int afid;
struct timeval when = { 1000, 0 };
char *fn = "func2";
unsigned int op;
pthread_barrier_wait(&barrier);
afid = __pmAFregister(&when, NULL, wakeup);
if (afid >= 0)
printf("%s: __pmAFregister -> OK\n", fn);
else
printf("%s: __pmAFregister -> %s\n", fn, pmErrStr(afid));
sts = __pmAFunregister(afid);
if (sts >= 0)
printf("%s: __pmAFunregister -> OK\n", fn);
else
printf("%s: __pmAFunregister -> %s\n", fn, pmErrStr(sts));
pthread_barrier_wait(&barrier);
pthread_barrier_wait(&barrier);
sts = __pmAccAddOp(1);
if (sts == 0)
printf("%s: __pmAccAddOp(1) -> OK\n", fn);
else
printf("%s: __pmAccAddOp(1) -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddOp(2);
if (sts == 0)
printf("%s: __pmAccAddOp(2) -> OK\n", fn);
else
printf("%s: __pmAccAddOp(2) -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddHost("localhost", 1, 2, 3);
if (sts == 0)
printf("%s: __pmAccAddHost -> OK\n", fn);
else
printf("%s: __pmAccAddHost -> %s\n", fn, pmErrStr(sts));
/* expect an error here - so no need for version-specific EOLs */
__pmAccDumpHosts(stdout);
sts = __pmAccSaveHosts();
if (sts == 0)
printf("%s: __pmAccSaveHosts -> OK\n", fn);
else
printf("%s: __pmAccSaveHosts -> %s\n", fn, pmErrStr(sts));
sts = __pmAccRestoreHosts();
if (sts == 0)
printf("%s: __pmAccRestoreHosts -> OK\n", fn);
else
printf("%s: __pmAccRestoreHosts -> %s\n", fn, pmErrStr(sts));
sts = __pmAccAddClient(addr, &op);
if (sts == 0)
printf("%s: __pmAccAddClient -> %d\n", fn, op);
else
printf("%s: __pmAccAddClient -> %s\n", fn, pmErrStr(sts));
pthread_exit(NULL);
}
int
main()
{
pthread_t tid1;
pthread_t tid2;
int sts;
char *msg;
#if PCP_VER >= 3702
addr = __pmLoopBackAddress(AF_INET);
#elif PCP_VER >= 3611
addr = __pmLoopBackAddress();
#else
addr = __pmLoopbackAddress();
#endif
sts = pthread_barrier_init(&barrier, NULL, 2);
if (sts != 0) {
printf("pthread_barrier_init: sts=%d\n", sts);
exit(1);
}
/* only need this to initialize library mutexes */
if ((sts = pmNewContext(PM_CONTEXT_LOCAL, NULL)) < 0) {
printf("pmNewContext: %s\n", pmErrStr(sts));
exit(1);
}
sts = pthread_create(&tid1, NULL, func1, NULL);
if (sts != 0) {
printf("thread_create: tid1: sts=%d\n", sts);
exit(1);
}
sts = pthread_create(&tid2, NULL, func2, NULL);
if (sts != 0) {
printf("thread_create: tid2: sts=%d\n", sts);
exit(1);
}
pthread_join(tid1, (void *)&msg);
if (msg != NULL) printf("tid1: %s\n", msg);
pthread_join(tid2, (void *)&msg);
if (msg != NULL) printf("tid2: %s\n", msg);
#if PCP_VER >= 3611
__pmSockAddrFree(addr);
#endif
exit(0);
}
|