-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVsamThread.cpp
98 lines (91 loc) · 2.55 KB
/
VsamThread.cpp
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
/*
* Licensed Materials - Property of IBM
* (C) Copyright IBM Corp. 2020, 2022. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
*/
#include <assert.h>
#include "VsamThread.h"
#ifdef DEBUG
static const char *getMessageStr(VSAM_THREAD_MSGID msgid) {
switch (msgid) {
case MSG_OPEN: return "OPEN";
case MSG_CLOSE: return "CLOSE";
case MSG_WRITE: return "WRITE";
case MSG_UPDATE: return "UPDATE";
case MSG_DELETE: return "DELETE";
case MSG_FIND: return "FIND";
case MSG_READ: return "READ";
case MSG_FIND_UPDATE: return "FIND_UPDATE";
case MSG_FIND_DELETE: return "FIND_DELETE";
case MSG_EXIT: return "EXIT";
default: return "UNKNOWN";
}
assert(0);
}
#endif
int gettid() { return (int)(pthread_self().__ & 0x7fffffff); }
void vsamThread(VsamFile *pVsamFile, std::condition_variable *pcv,
std::mutex *pmtx, std::queue<ST_VsamThreadMsg *> *pqueue) {
#ifdef DEBUG
fprintf(stderr, "vsamThread tid=%d started.\n", gettid());
#endif
ST_VsamThreadMsg *pmsg;
while (1) {
std::unique_lock<std::mutex> lck(*pmtx);
while (pqueue->empty())
pcv->wait(lck);
if (pqueue->empty())
continue;
pmsg = pqueue->front();
pqueue->pop();
#if defined(DEBUG) || defined(DEBUG_CRUD)
fflush(stderr);
fflush(stdout);
fprintf(stderr, "vsamThread tid=%d got message %s.\n", gettid(),
getMessageStr(pmsg->msgid));
#endif
switch (pmsg->msgid) {
case MSG_OPEN:
case MSG_CLOSE:
case MSG_WRITE:
case MSG_UPDATE:
case MSG_DELETE:
case MSG_FIND:
case MSG_READ:
case MSG_FIND_UPDATE:
case MSG_FIND_DELETE:
(pVsamFile->*(pmsg->pWorkFunc))(pmsg->pdata);
pmsg->rc = pmsg->pdata->rc_;
if (pmsg->msgid == MSG_CLOSE) {
pVsamFile->detachVsamThread();
pmsg->cv.notify_one();
#ifdef DEBUG
fprintf(stderr, "vsamThread tid=%d terminating after message CLOSE.\n",
gettid());
#endif
return;
}
pmsg->cv.notify_one();
break;
case MSG_EXIT:
pmsg->rc = 0;
pVsamFile->detachVsamThread();
pmsg->cv.notify_one();
#ifdef DEBUG
fprintf(stderr, "vsamThread tid=%d terminating after message EXIT.\n",
gettid());
#endif
return;
default:
#ifdef DEBUG
fprintf(stderr,
"vsamThread tid=%d terminating after message UNKNOWN (%d)",
gettid(), pmsg->msgid);
#endif
pVsamFile->detachVsamThread();
pmsg->cv.notify_one();
assert(0);
}
}
}