osSemaphore.c 5.7 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;
}

wafwerar's avatar
wafwerar 已提交
78
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
79
  struct timespec ts, rel;
L
Liu Jicong 已提交
80 81
  FILETIME        ft_before, ft_after;
  int             rc;
wafwerar's avatar
wafwerar 已提交
82 83 84 85 86

  rel.tv_sec = 0;
  rel.tv_nsec = nanosecs;

  GetSystemTimeAsFileTime(&ft_before);
wafwerar's avatar
wafwerar 已提交
87 88
  // errno = 0;
  rc = sem_timedwait(sem, pthread_win32_getabstime_np(&ts, &rel));
wafwerar's avatar
wafwerar 已提交
89 90

  /* This should have timed out */
wafwerar's avatar
wafwerar 已提交
91 92 93 94 95 96
  // 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 已提交
97
  //     printf("nanoseconds: %d, rc: %d, code:0x%x. before filetime: %d, %d; after filetime: %d, %d\n",
wafwerar's avatar
wafwerar 已提交
98 99 100 101 102 103 104
  //         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;
wafwerar's avatar
wafwerar 已提交
105 106
}

S
Shengliang Guan 已提交
107 108
#elif defined(_TD_DARWIN_64)

F
freemine 已提交
109 110
#include <libproc.h>

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

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

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

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

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

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

F
freemine 已提交
144
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
145 146
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
147 148
}

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

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

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

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

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

S
Shengliang Guan 已提交
168 169 170 171 172 173 174
#else

/*
 * linux implementation
 */

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

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

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

wafwerar's avatar
wafwerar 已提交
186 187 188
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 已提交
189 190

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

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

S
Shengliang Guan 已提交
201 202 203 204 205 206 207 208 209 210 211 212
  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 已提交
213
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  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 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
  int ret = 0;

  struct timespec tv = {
      .tv_sec = 0,
      .tv_nsec = nanosecs,
  };

  while ((ret = sem_timedwait(sem, &tv)) == -1 && errno == EINTR) continue;

  return ret;
}

S
Shengliang Guan 已提交
243
#endif