osSemaphore.c 5.8 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
slguan 已提交
17 18
#define _DEFAULT_SOURCE
#include "os.h"
wafwerar's avatar
wafwerar 已提交
19
#include "pthread.h"
L
Liu Jicong 已提交
20
#include "tdef.h"
S
Shengliang Guan 已提交
21

wafwerar's avatar
wafwerar 已提交
22
#ifdef WINDOWS
S
Shengliang Guan 已提交
23 24 25 26 27 28 29

/*
 * windows implementation
 */

#include <windows.h>

wafwerar's avatar
wafwerar 已提交
30
bool taosCheckPthreadValid(TdThread thread) { return thread.p != NULL; }
S
Shengliang Guan 已提交
31

wafwerar's avatar
wafwerar 已提交
32
void taosResetPthread(TdThread* thread) { thread->p = 0; }
S
Shengliang Guan 已提交
33

wafwerar's avatar
wafwerar 已提交
34
int64_t taosGetPthreadId(TdThread thread) {
S
Shengliang Guan 已提交
35 36 37 38 39 40 41 42 43
#ifdef PTW32_VERSION
  return pthread_getw32threadid_np(thread);
#else
  return (int64_t)thread;
#endif
}

int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); }

wafwerar's avatar
wafwerar 已提交
44
bool taosComparePthread(TdThread first, TdThread second) { return first.p == second.p; }
S
Shengliang Guan 已提交
45 46 47

int32_t taosGetPId() { return GetCurrentProcessId(); }

48
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
49 50 51 52 53 54 55
  char filepath[1024] = {0};

  GetModuleFileName(NULL, filepath, MAX_PATH);
  char* sub = strrchr(filepath, '.');
  if (sub != NULL) {
    *sub = '\0';
  }
wafwerar's avatar
wafwerar 已提交
56 57 58 59 60
  char* end = strrchr(filepath, TD_DIRSEP[0]);
  if (end == NULL) {
    end = filepath;
  }

wafwerar's avatar
wafwerar 已提交
61
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
62 63

  if (len != NULL) {
wafwerar's avatar
wafwerar 已提交
64
    *len = (int32_t)strlen(end);
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

L
Liu Jicong 已提交
78
int32_t tsem_timewait(tsem_t* sem, int64_t milis) {
L
Liu Jicong 已提交
79 80
  return 0;
  /*return tsem_wait(sem);*/
L
Liu Jicong 已提交
81 82 83 84 85 86 87 88
#if 0
  struct timespec ts;
  timespec_get(&ts);
  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;

  /*GetSystemTimeAsFileTime(&ft_before);*/
wafwerar's avatar
wafwerar 已提交
89
  // errno = 0;
L
Liu Jicong 已提交
90
  rc = sem_timedwait(sem, ts);
wafwerar's avatar
wafwerar 已提交
91 92

  /* This should have timed out */
wafwerar's avatar
wafwerar 已提交
93 94 95 96 97 98
  // assert(errno == ETIMEDOUT);
  // assert(rc != 0);
  // GetSystemTimeAsFileTime(&ft_after);
  // // We specified a non-zero wait. Time must advance.
  // if (ft_before.dwLowDateTime == ft_after.dwLowDateTime && ft_before.dwHighDateTime == ft_after.dwHighDateTime)
  //   {
S
Shengliang Guan 已提交
99
  //     printf("nanoseconds: %d, rc: %d, code:0x%x. before filetime: %d, %d; after filetime: %d, %d\n",
wafwerar's avatar
wafwerar 已提交
100 101 102 103 104 105 106
  //         nanosecs, rc, errno,
  //         (int)ft_before.dwLowDateTime, (int)ft_before.dwHighDateTime,
  //         (int)ft_after.dwLowDateTime, (int)ft_after.dwHighDateTime);
  //     printf("time must advance during sem_timedwait.");
  //     return 1;
  //   }
  return rc;
L
Liu Jicong 已提交
107
#endif
wafwerar's avatar
wafwerar 已提交
108 109
}

S
Shengliang Guan 已提交
110 111
#elif defined(_TD_DARWIN_64)

F
freemine 已提交
112 113
#include <libproc.h>

L
Liu Jicong 已提交
114
int tsem_init(tsem_t *psem, int flags, unsigned int count) {
wafwerar's avatar
wafwerar 已提交
115 116
  *psem = dispatch_semaphore_create(count);
  if (*psem == NULL) return -1;
L
Liu Jicong 已提交
117
  return 0;
118 119
}

L
Liu Jicong 已提交
120
int tsem_destroy(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
121
  if (psem == NULL || *psem == NULL) return -1;
wafwerar's avatar
wafwerar 已提交
122 123
  // dispatch_release(*psem);
  // *psem = NULL;
L
Liu Jicong 已提交
124
  return 0;
125 126
}

L
Liu Jicong 已提交
127
int tsem_post(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
128 129
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_signal(*psem);
L
Liu Jicong 已提交
130
  return 0;
wafwerar's avatar
wafwerar 已提交
131
}
F
freemine 已提交
132

L
Liu Jicong 已提交
133
int tsem_wait(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
134 135
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER);
L
Liu Jicong 已提交
136
  return 0;
S
slguan 已提交
137
}
F
freemine 已提交
138

L
Liu Jicong 已提交
139
int tsem_timewait(tsem_t *psem, int64_t milis) {
wafwerar's avatar
wafwerar 已提交
140
  if (psem == NULL || *psem == NULL) return -1;
L
Liu Jicong 已提交
141
  dispatch_semaphore_wait(*psem, milis * 1000 * 1000);
L
Liu Jicong 已提交
142
  return 0;
F
freemine 已提交
143 144
}

wafwerar's avatar
wafwerar 已提交
145
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
wafwerar's avatar
wafwerar 已提交
146

F
freemine 已提交
147
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
148 149
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
150 151
}

wafwerar's avatar
wafwerar 已提交
152
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
F
freemine 已提交
153

wafwerar's avatar
wafwerar 已提交
154
void taosResetPthread(TdThread *thread) { *thread = NULL; }
F
freemine 已提交
155

wafwerar's avatar
wafwerar 已提交
156
bool taosComparePthread(TdThread first, TdThread second) { return taosThreadEqual(first, second) ? true : false; }
F
freemine 已提交
157

S
Shengliang Guan 已提交
158
int32_t taosGetPId() { return (int32_t)getpid(); }
F
freemine 已提交
159

160
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
161
  char buf[PATH_MAX + 1];
F
freemine 已提交
162
  buf[0] = '\0';
S
Shengliang Guan 已提交
163
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
164 165 166
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
wafwerar's avatar
wafwerar 已提交
167
  if (name) tstrncpy(name, buf, TSDB_APP_NAME_LEN);
F
freemine 已提交
168 169 170
  return 0;
}

S
Shengliang Guan 已提交
171 172 173 174 175 176 177
#else

/*
 * linux implementation
 */

#include <sys/syscall.h>
S
Shengliang Guan 已提交
178
#include <unistd.h>
S
Shengliang Guan 已提交
179

wafwerar's avatar
wafwerar 已提交
180
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
181 182 183 184 185 186 187 188

int64_t taosGetSelfPthreadId() {
  static __thread int id = 0;
  if (id != 0) return id;
  id = syscall(SYS_gettid);
  return id;
}

wafwerar's avatar
wafwerar 已提交
189 190 191
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
void    taosResetPthread(TdThread* thread) { *thread = 0; }
bool    taosComparePthread(TdThread first, TdThread second) { return first == second; }
L
Liu Jicong 已提交
192 193

int32_t taosGetPId() {
L
Liu Jicong 已提交
194
  static int32_t pid;
L
Liu Jicong 已提交
195 196 197 198
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
199

200
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
201 202
  const char* self = "/proc/self/exe";
  char        path[PATH_MAX] = {0};
F
freemine 已提交
203

S
Shengliang Guan 已提交
204 205 206 207 208 209 210 211 212 213 214 215
  if (readlink(self, path, PATH_MAX) <= 0) {
    return -1;
  }

  path[PATH_MAX - 1] = 0;
  char* end = strrchr(path, '/');
  if (end == NULL) {
    return -1;
  }

  ++end;

wafwerar's avatar
wafwerar 已提交
216
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

  if (len != NULL) {
    *len = strlen(name);
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

L
Liu Jicong 已提交
233
int32_t tsem_timewait(tsem_t* sem, int64_t ms) {
L
Liu Jicong 已提交
234 235
  int ret = 0;

L
Liu Jicong 已提交
236 237 238 239 240 241 242 243 244
  struct timespec ts = {0};

  if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
    return -1;
  }

  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;
L
Liu Jicong 已提交
245

L
Liu Jicong 已提交
246
  while ((ret = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) continue;
L
Liu Jicong 已提交
247 248 249 250

  return ret;
}

S
Shengliang Guan 已提交
251
#endif