PikaPlatform.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "PikaPlatform.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #if defined(_WIN32) && !defined(CROSS_BUILD)
  31. #include <Windows.h>
  32. #include <io.h>
  33. #endif
  34. #if defined(_WIN32)
  35. #include <direct.h>
  36. #endif
  37. #if defined(__linux) || PIKA_LINUX_COMPATIBLE
  38. #include <dirent.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. #include "unistd.h"
  42. #endif
  43. #if (defined(__linux) || PIKA_LINUX_COMPATIBLE)
  44. #include <dirent.h>
  45. #endif
  46. #if PIKA_WIN_PTHREAD_ENABLE
  47. struct timeval {
  48. long tv_sec; // Seconds
  49. long tv_usec; // Microseconds
  50. };
  51. static void usleep(unsigned long usec) {
  52. HANDLE timer;
  53. LARGE_INTEGER interval;
  54. interval.QuadPart = (10 * usec);
  55. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  56. SetWaitableTimer(timer, &interval, 0, NULL, NULL, 0);
  57. WaitForSingleObject(timer, INFINITE);
  58. CloseHandle(timer);
  59. }
  60. static int gettimeofday(struct timeval* tp, void* tzp) {
  61. time_t clock;
  62. struct tm tm;
  63. SYSTEMTIME wtm;
  64. GetLocalTime(&wtm);
  65. tm.tm_year = wtm.wYear - 1900;
  66. tm.tm_mon = wtm.wMonth - 1;
  67. tm.tm_mday = wtm.wDay;
  68. tm.tm_hour = wtm.wHour;
  69. tm.tm_min = wtm.wMinute;
  70. tm.tm_sec = wtm.wSecond;
  71. tm.tm_isdst = -1;
  72. clock = mktime(&tm);
  73. tp->tv_sec = clock;
  74. tp->tv_usec = wtm.wMilliseconds * 1000;
  75. return (0);
  76. }
  77. static void timeradd(struct timeval* a,
  78. struct timeval* b,
  79. struct timeval* res) {
  80. res->tv_sec = a->tv_sec + b->tv_sec;
  81. res->tv_usec = a->tv_usec + b->tv_usec;
  82. if (res->tv_usec >= 1000000) {
  83. res->tv_sec += res->tv_usec / 1000000;
  84. res->tv_usec %= 1000000;
  85. }
  86. }
  87. static void timersub(struct timeval* a,
  88. struct timeval* b,
  89. struct timeval* res) {
  90. res->tv_sec = a->tv_sec - b->tv_sec;
  91. res->tv_usec = a->tv_usec - b->tv_usec;
  92. if (res->tv_usec < 0) {
  93. res->tv_sec -= 1;
  94. res->tv_usec += 1000000;
  95. }
  96. }
  97. #endif
  98. void pikaFree(void* mem, uint32_t size);
  99. void* pikaMalloc(uint32_t size);
  100. int pika_pvsprintf(char** buff, const char* fmt, va_list args);
  101. /* stdio platform */
  102. int pika_putchar(char ch) {
  103. int ret = pika_platform_putchar(ch);
  104. #if PIKA_UNBUFFERED_ENABLE
  105. pika_platform_fflush(stdout);
  106. #endif
  107. return ret;
  108. }
  109. PIKA_WEAK void pika_platform_clear(void) {
  110. pika_platform_remove(PIKA_SHELL_SAVE_APP_PATH);
  111. pika_platform_reboot();
  112. }
  113. #if !PIKA_PLATFORM_NO_WEAK
  114. PIKA_WEAK void pika_platform_disable_irq_handle(void) {
  115. /* disable irq to support thread */
  116. }
  117. PIKA_WEAK void pika_platform_enable_irq_handle(void) {
  118. /* disable irq to support thread */
  119. }
  120. /* memory support */
  121. PIKA_WEAK void* pika_platform_malloc(size_t size) {
  122. return malloc(size);
  123. }
  124. PIKA_WEAK void* pika_platform_realloc(void* ptr, size_t size) {
  125. return realloc(ptr, size);
  126. }
  127. void* pika_reallocn(void* ptr, size_t size_old, size_t size_new) {
  128. void* new_ptr = pika_platform_malloc(size_new);
  129. if (new_ptr) {
  130. if (ptr) {
  131. pika_platform_memcpy(new_ptr, ptr, size_old);
  132. pika_platform_free(ptr);
  133. }
  134. }
  135. return new_ptr;
  136. }
  137. PIKA_WEAK void* pika_platform_calloc(size_t num, size_t size) {
  138. void* ptr = pika_platform_malloc(num * size);
  139. if (ptr) {
  140. pika_platform_memset(ptr, 0, num * size);
  141. }
  142. return ptr;
  143. }
  144. PIKA_WEAK void pika_platform_free(void* ptr) {
  145. free(ptr);
  146. }
  147. PIKA_WEAK void* pika_user_malloc(size_t size) {
  148. return pika_platform_malloc(size);
  149. }
  150. PIKA_WEAK void pika_user_free(void* ptr, size_t size) {
  151. pika_platform_free(ptr);
  152. }
  153. PIKA_WEAK uint8_t pika_is_locked_pikaMemory(void) {
  154. return 0;
  155. }
  156. /* time support */
  157. #if PIKA_FREERTOS_ENABLE
  158. static uint32_t platform_uptime_ms(void) {
  159. TickType_t tick = 0u;
  160. tick = xTaskGetTickCount() * 1000;
  161. return (uint32_t)((tick + configTICK_RATE_HZ - 1) / configTICK_RATE_HZ);
  162. }
  163. #endif
  164. PIKA_WEAK void pika_platform_sleep_ms(uint32_t ms) {
  165. #if defined(__linux)
  166. usleep(ms * 1000);
  167. #elif defined(_WIN32) && !defined(CROSS_BUILD)
  168. Sleep(ms);
  169. #elif PIKA_RTTHREAD_ENABLE
  170. rt_thread_mdelay(ms);
  171. #elif PIKA_ZEUSOS_ENABLE
  172. zos_task_msleep(ms);
  173. #elif PIKA_FREERTOS_ENABLE
  174. vTaskDelay(ms / portTICK_PERIOD_MS);
  175. #else
  176. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  177. #endif
  178. }
  179. PIKA_WEAK void pika_platform_sleep_us(uint32_t us) {
  180. volatile uint32_t i = 0;
  181. for (i = 0; i < us; i++) {
  182. volatile uint32_t timeout = 100;
  183. while (timeout--) {
  184. }
  185. }
  186. }
  187. PIKA_WEAK int64_t pika_platform_get_tick(void) {
  188. #if PIKA_FREERTOS_ENABLE
  189. return platform_uptime_ms();
  190. #elif defined(__linux)
  191. struct timespec ts;
  192. clock_gettime(CLOCK_REALTIME, &ts);
  193. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  194. #elif PIKA_RTTHREAD_ENABLE
  195. uint32_t tick = rt_tick_get() * 1000;
  196. return (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND);
  197. #elif PIKA_ZEUSOS_ENABLE
  198. uint32_t tick = zos_tick_get() * 1000;
  199. return (uint32_t)((tick + ZOS_TICK_PER_SECOND - 1) / ZOS_TICK_PER_SECOND);
  200. #elif defined(_WIN32) && !defined(CROSS_BUILD)
  201. FILETIME ft;
  202. ULARGE_INTEGER ull;
  203. GetSystemTimeAsFileTime(&ft);
  204. ull.LowPart = ft.dwLowDateTime;
  205. ull.HighPart = ft.dwHighDateTime;
  206. ull.QuadPart -= 116444736000000000;
  207. ull.QuadPart /= 10000;
  208. return ull.QuadPart;
  209. #else
  210. return -1;
  211. #endif
  212. }
  213. PIKA_WEAK int pika_platform_fflush(void* stream) {
  214. #if PIKA_UNBUFFERED_ENABLE
  215. return fflush(stream);
  216. #else
  217. return 0;
  218. #endif
  219. }
  220. PIKA_WEAK int pika_platform_putchar(char ch) {
  221. return putchar(ch);
  222. }
  223. #ifndef pika_platform_printf
  224. PIKA_WEAK void pika_platform_printf(char* fmt, ...) {
  225. va_list args;
  226. va_start(args, fmt);
  227. pika_vprintf(fmt, args);
  228. va_end(args);
  229. }
  230. #endif
  231. PIKA_WEAK char* pika_platform_strdup(const char* src) {
  232. char* dst = (char*)pika_platform_malloc(strlen(src) + 1);
  233. if (dst) {
  234. strcpy(dst, src);
  235. }
  236. return dst;
  237. }
  238. PIKA_WEAK size_t pika_platform_tick_from_millisecond(size_t ms) {
  239. return ms;
  240. }
  241. PIKA_WEAK int pika_platform_vsnprintf(char* buff,
  242. size_t size,
  243. const char* fmt,
  244. va_list args) {
  245. return vsnprintf(buff, size, fmt, args);
  246. }
  247. PIKA_WEAK void pika_platform_wait(void) {
  248. while (1) {
  249. };
  250. }
  251. PIKA_WEAK void* pika_platform_memset(void* mem, int ch, size_t size) {
  252. return memset(mem, ch, size);
  253. }
  254. PIKA_WEAK void* pika_platform_memcpy(void* dir, const void* src, size_t size) {
  255. return memcpy(dir, src, size);
  256. }
  257. PIKA_WEAK int pika_platform_memcmp(const void* s1, const void* s2, size_t n) {
  258. return memcmp(s1, s2, n);
  259. }
  260. PIKA_WEAK void* pika_platform_memmove(void* s1, void* s2, size_t n) {
  261. return memmove(s1, s2, n);
  262. }
  263. PIKA_WEAK char pika_platform_getchar(void) {
  264. #if defined(__linux) || defined(_WIN32)
  265. return getchar();
  266. #else
  267. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  268. #endif
  269. }
  270. /* return -1 for no char received, 0 for received */
  271. PIKA_WEAK int pika_platform_getchar_nonblocking(char* ch) {
  272. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  273. }
  274. PIKA_WEAK int pika_platform_repl_recv(uint8_t* buff,
  275. size_t size,
  276. uint32_t timeout) {
  277. if (timeout != PIKA_TIMEOUT_FOREVER) {
  278. pika_platform_printf(
  279. "Error: timeout not support for default pika_platform_repl_recv, "
  280. "please override it\n");
  281. return -1;
  282. }
  283. for (size_t i = 0; i < size; i++) {
  284. buff[i] = pika_platform_getchar();
  285. }
  286. return size;
  287. }
  288. /* file system support */
  289. PIKA_WEAK FILE* pika_platform_fopen(const char* filename, const char* modes) {
  290. #if defined(__linux) || defined(_WIN32)
  291. return fopen(filename, modes);
  292. #else
  293. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  294. #endif
  295. }
  296. PIKA_WEAK int pika_platform_fclose(FILE* stream) {
  297. #if defined(__linux) || defined(_WIN32)
  298. return fclose(stream);
  299. #else
  300. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  301. #endif
  302. }
  303. PIKA_WEAK size_t pika_platform_fwrite(const void* ptr,
  304. size_t size,
  305. size_t n,
  306. FILE* stream) {
  307. pika_assert(NULL != stream);
  308. #if defined(__linux) || defined(_WIN32)
  309. return fwrite(ptr, size, n, stream);
  310. #else
  311. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  312. #endif
  313. }
  314. PIKA_WEAK size_t pika_platform_fread(void* ptr,
  315. size_t size,
  316. size_t n,
  317. FILE* stream) {
  318. #if defined(__linux) || defined(_WIN32)
  319. return fread(ptr, size, n, stream);
  320. #else
  321. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  322. #endif
  323. }
  324. PIKA_WEAK int pika_platform_fseek(FILE* stream, long offset, int whence) {
  325. #if defined(__linux) || defined(_WIN32)
  326. return fseek(stream, offset, whence);
  327. #else
  328. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  329. #endif
  330. }
  331. PIKA_WEAK long pika_platform_ftell(FILE* stream) {
  332. #if defined(__linux) || defined(_WIN32)
  333. return ftell(stream);
  334. #else
  335. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  336. #endif
  337. }
  338. PIKA_WEAK char* pika_platform_getcwd(char* buf, size_t size) {
  339. #if defined(__linux) || defined(_WIN32)
  340. return getcwd(buf, size);
  341. #else
  342. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  343. #endif
  344. }
  345. PIKA_WEAK int pika_platform_chdir(const char* path) {
  346. #if defined(__linux) || defined(_WIN32)
  347. return chdir(path);
  348. #else
  349. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  350. #endif
  351. }
  352. PIKA_WEAK int pika_platform_rmdir(const char* pathname) {
  353. #if defined(__linux) || defined(_WIN32)
  354. return rmdir(pathname);
  355. #else
  356. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  357. #endif
  358. }
  359. PIKA_WEAK int pika_platform_mkdir(const char* pathname, int mode) {
  360. #if defined(_WIN32) && !defined(CROSS_BUILD)
  361. (void)(mode);
  362. return mkdir(pathname);
  363. #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
  364. return mkdir(pathname, mode);
  365. #else
  366. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  367. #endif
  368. }
  369. PIKA_WEAK char* pika_platform_realpath(const char* path, char* resolved_path) {
  370. #if defined(__linux) || PIKA_LINUX_COMPATIBLE
  371. return realpath(path, resolved_path);
  372. #else
  373. if (!path || !resolved_path)
  374. return NULL;
  375. char* output = resolved_path;
  376. const char* segment_start = path;
  377. const char* segment_end = path;
  378. while (*segment_end) {
  379. if (*segment_end == '/' || *(segment_end + 1) == '\0') {
  380. size_t segment_len =
  381. segment_end - segment_start + (*segment_end != '/');
  382. if (segment_len == 1 && segment_start[0] == '.') {
  383. // Skip single-dot segment
  384. } else if (segment_len == 2 && segment_start[0] == '.' &&
  385. segment_start[1] == '.') {
  386. // Handle double-dot segment by backtracking
  387. if (output > resolved_path) {
  388. output--; // Move back one char to overwrite the last slash
  389. while (output > resolved_path && *output != '/') {
  390. output--;
  391. }
  392. }
  393. } else {
  394. // Copy the segment to the output
  395. strncpy(output, segment_start, segment_len);
  396. output += segment_len;
  397. if (*segment_end) {
  398. *output = '/';
  399. output++;
  400. }
  401. }
  402. segment_end++; // Move past the slash
  403. segment_start = segment_end;
  404. } else {
  405. segment_end++;
  406. }
  407. }
  408. if (output != resolved_path && *(output - 1) == '/') {
  409. output--; // Remove trailing slash, if any
  410. }
  411. *output = '\0'; // Null-terminate the resolved path
  412. return resolved_path;
  413. #endif
  414. }
  415. PIKA_WEAK int pika_platform_path_exists(const char* path) {
  416. #if defined(_WIN32) && !defined(CROSS_BUILD)
  417. DWORD attr = GetFileAttributesA((LPCSTR)path);
  418. if (attr == INVALID_FILE_ATTRIBUTES) {
  419. return 0;
  420. }
  421. return 1;
  422. #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
  423. struct stat statbuf;
  424. if (stat(path, &statbuf) == -1) {
  425. return 0;
  426. }
  427. return 1;
  428. #else
  429. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  430. #endif
  431. }
  432. PIKA_WEAK int pika_platform_path_isdir(const char* path) {
  433. #if defined(_WIN32) && !defined(CROSS_BUILD)
  434. int is_dir = 0;
  435. DWORD attrs = GetFileAttributes((LPCSTR)path);
  436. if (attrs != INVALID_FILE_ATTRIBUTES) {
  437. is_dir = (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? 1 : 0;
  438. }
  439. return is_dir;
  440. #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
  441. int is_dir = 0;
  442. struct stat st;
  443. if (stat(path, &st) == 0) {
  444. is_dir = S_ISDIR(st.st_mode) ? PIKA_TRUE : PIKA_FALSE;
  445. }
  446. return is_dir;
  447. #else
  448. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  449. #endif
  450. }
  451. // Returns true if the given path is a regular file, false otherwise.
  452. PIKA_WEAK int pika_platform_path_isfile(const char* path) {
  453. #if defined(_WIN32) && !defined(CROSS_BUILD)
  454. int is_file = 0;
  455. DWORD attrs = GetFileAttributes(path);
  456. if (attrs != INVALID_FILE_ATTRIBUTES) {
  457. is_file =
  458. (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0 ? PIKA_TRUE : PIKA_FALSE;
  459. }
  460. return is_file;
  461. #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
  462. int is_file = 0;
  463. struct stat st;
  464. if (stat(path, &st) == 0) {
  465. is_file = S_ISREG(st.st_mode) ? PIKA_TRUE : PIKA_FALSE;
  466. }
  467. return is_file;
  468. #else
  469. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
  470. #endif
  471. }
  472. PIKA_WEAK int pika_platform_remove(const char* pathname) {
  473. #if defined(__linux) || defined(_WIN32)
  474. return remove(pathname);
  475. #else
  476. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  477. #endif
  478. }
  479. PIKA_WEAK int pika_platform_rename(const char* oldpath, const char* newpath) {
  480. #if defined(__linux) || defined(_WIN32)
  481. return rename(oldpath, newpath);
  482. #else
  483. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  484. #endif
  485. }
  486. PIKA_WEAK char** pika_platform_listdir(const char* path, int* count) {
  487. #if defined(__linux)
  488. struct dirent* dp;
  489. DIR* dir = opendir(path);
  490. char** filenames = NULL;
  491. *count = 0;
  492. if (dir != NULL) {
  493. while ((dp = readdir(dir)) != NULL) {
  494. if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
  495. size_t size_old = (*count) * sizeof(char*);
  496. size_t size_new = (size_old) + sizeof(char*);
  497. filenames =
  498. (char**)pika_reallocn(filenames, size_old, size_new);
  499. filenames[*count] = pika_platform_strdup(dp->d_name);
  500. (*count)++;
  501. }
  502. }
  503. closedir(dir);
  504. }
  505. return filenames;
  506. #elif defined(_WIN32) && !defined(CROSS_BUILD)
  507. struct _finddata_t fb;
  508. intptr_t handle = 0;
  509. char dirpath[256] = {0};
  510. char* currentPath = _getcwd(dirpath, 256);
  511. strcat(dirpath, path);
  512. strcat(dirpath, "\\*");
  513. char** filenames = NULL;
  514. *count = 0;
  515. handle = _findfirst(dirpath, &fb);
  516. if (handle != -1L) {
  517. do {
  518. if (strcmp(fb.name, ".") != 0 && strcmp(fb.name, "..") != 0) {
  519. size_t size_old = (*count) * sizeof(char*);
  520. size_t size_new = (size_old) + sizeof(char*);
  521. filenames =
  522. (char**)pika_reallocn(filenames, size_old, size_new);
  523. filenames[*count] = pika_platform_strdup(fb.name);
  524. (*count)++;
  525. }
  526. } while (_findnext(handle, &fb) == 0);
  527. _findclose(handle);
  528. }
  529. return filenames;
  530. #else
  531. WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
  532. #endif
  533. }
  534. /* thread support */
  535. PIKA_WEAK void pika_platform_thread_yield(void) {
  536. pika_thread_idle_hook();
  537. #if PIKA_FREERTOS_ENABLE
  538. vTaskDelay(1);
  539. #elif defined(_WIN32)
  540. SwitchToThread();
  541. #elif defined(__linux)
  542. sched_yield();
  543. #elif PIKA_RTTHREAD_ENABLE
  544. rt_thread_yield();
  545. #elif PIKA_ZEUSOS_ENABLE
  546. zos_task_msleep(1);
  547. #else
  548. return;
  549. #endif
  550. }
  551. PIKA_WEAK pika_platform_thread_t* pika_platform_thread_init(
  552. const char* name,
  553. void (*entry)(void*),
  554. void* const param,
  555. unsigned int stack_size,
  556. unsigned int priority,
  557. unsigned int tick) {
  558. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  559. int res;
  560. pika_platform_thread_t* thread;
  561. void* (*thread_entry)(void*);
  562. thread_entry = (void* (*)(void*))entry;
  563. thread = pikaMalloc(sizeof(pika_platform_thread_t));
  564. res = pthread_create(&thread->thread, NULL, thread_entry, param);
  565. if (res != 0) {
  566. pikaFree(thread, sizeof(pika_platform_thread_t));
  567. }
  568. thread->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
  569. thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
  570. return thread;
  571. #elif PIKA_FREERTOS_ENABLE
  572. pika_platform_thread_t* thread;
  573. thread = pikaMalloc(sizeof(pika_platform_thread_t));
  574. #if PIKA_THREAD_MALLOC_STACK_ENABLE
  575. thread->thread_stack_size = stack_size;
  576. thread->thread_stack = pikaMalloc(thread->thread_stack_size);
  577. #endif
  578. (void)tick;
  579. #if PIKA_THREAD_MALLOC_STACK_ENABLE
  580. thread->thread =
  581. xTaskCreateStatic(entry, name, stack_size, param, priority,
  582. thread->thread_stack, &thread->task_buffer);
  583. #else
  584. int err =
  585. xTaskCreate(entry, name, stack_size, param, priority, &thread->thread);
  586. #endif
  587. #if PIKA_THREAD_MALLOC_STACK_ENABLE
  588. if (NULL == thread->thread) {
  589. pikaFree(thread->thread_stack, thread->thread_stack_size);
  590. pikaFree(thread, sizeof(pika_platform_thread_t));
  591. return NULL;
  592. }
  593. #else
  594. if (pdPASS != err) {
  595. pikaFree(thread, sizeof(pika_platform_thread_t));
  596. return NULL;
  597. }
  598. #endif
  599. return thread;
  600. #elif PIKA_RTTHREAD_ENABLE
  601. pika_platform_thread_t* thread;
  602. thread = pikaMalloc(sizeof(pika_platform_thread_t));
  603. if (RT_NULL == thread) {
  604. return RT_NULL;
  605. }
  606. thread->thread = rt_thread_create((const char*)name, entry, param,
  607. stack_size, priority, tick);
  608. if (thread->thread == RT_NULL) {
  609. pikaFree(thread, sizeof(pika_platform_thread_t));
  610. return RT_NULL;
  611. } else {
  612. return thread;
  613. }
  614. #elif PIKA_ZEUSOS_ENABLE
  615. pika_platform_thread_t* thread;
  616. static int thread_count = 0;
  617. char task_name[ZOS_NAME_MAX + 1] = {0};
  618. zos_sprintf(task_name, "%s%d", name, thread_count++);
  619. thread = pikaMalloc(sizeof(pika_platform_thread_t));
  620. if (ZOS_NULL == thread) {
  621. return ZOS_NULL;
  622. }
  623. thread->thread =
  624. zos_task_create(task_name, entry, param, stack_size, priority);
  625. if (thread->thread == ZOS_NULL) {
  626. pikaFree(thread, sizeof(pika_platform_thread_t));
  627. return ZOS_NULL;
  628. } else {
  629. return thread;
  630. }
  631. #else
  632. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  633. return NULL;
  634. #endif
  635. }
  636. PIKA_WEAK uint64_t pika_platform_thread_self(void) {
  637. #if defined(__linux)
  638. return (uint64_t)pthread_self();
  639. #elif PIKA_WIN_PTHREAD_ENABLE
  640. return (uint64_t)(pthread_self().p);
  641. #elif PIKA_FREERTOS_ENABLE
  642. return (uint64_t)xTaskGetCurrentTaskHandle();
  643. #elif PIKA_RTTHREAD_ENABLE
  644. return (uint64_t)(uintptr_t)rt_thread_self();
  645. #elif PIKA_ZEUSOS_ENABLE
  646. return (uint64_t)zos_task_self();
  647. #else
  648. return 0;
  649. #endif
  650. }
  651. PIKA_WEAK void pika_platform_thread_startup(pika_platform_thread_t* thread) {
  652. (void)thread;
  653. #if PIKA_RTTHREAD_ENABLE
  654. rt_thread_startup(thread->thread);
  655. #elif PIKA_ZEUSOS_ENABLE
  656. zos_task_startup(thread->thread);
  657. #endif
  658. }
  659. PIKA_WEAK void pika_platform_thread_stop(pika_platform_thread_t* thread) {
  660. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  661. pthread_mutex_lock(&(thread->mutex));
  662. pthread_cond_wait(&(thread->cond), &(thread->mutex));
  663. pthread_mutex_unlock(&(thread->mutex));
  664. #elif PIKA_FREERTOS_ENABLE
  665. vTaskSuspend(thread->thread);
  666. #elif PIKA_RTTHREAD_ENABLE
  667. rt_thread_suspend(thread->thread);
  668. #elif PIKA_ZEUSOS_ENABLE
  669. zos_task_suspend(thread->thread);
  670. #else
  671. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  672. #endif
  673. }
  674. PIKA_WEAK void pika_platform_thread_start(pika_platform_thread_t* thread) {
  675. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  676. pthread_mutex_lock(&(thread->mutex));
  677. pthread_cond_signal(&(thread->cond));
  678. pthread_mutex_unlock(&(thread->mutex));
  679. #elif PIKA_FREERTOS_ENABLE
  680. vTaskResume(thread->thread);
  681. #elif PIKA_RTTHREAD_ENABLE
  682. rt_thread_resume(thread->thread);
  683. #elif PIKA_ZEUSOS_ENABLE
  684. zos_task_resume(thread->thread);
  685. #else
  686. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  687. #endif
  688. }
  689. PIKA_WEAK void pika_platform_thread_destroy(pika_platform_thread_t* thread) {
  690. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  691. if (NULL != thread) {
  692. pthread_detach(thread->thread);
  693. // pthread_join(thread->thread, NULL);
  694. pikaFree(thread, sizeof(pika_platform_thread_t));
  695. thread = NULL;
  696. return;
  697. }
  698. #elif PIKA_FREERTOS_ENABLE
  699. if (NULL != thread) {
  700. vTaskDelete(thread->thread);
  701. pikaFree(thread, sizeof(pika_platform_thread_t));
  702. return;
  703. }
  704. #elif PIKA_RTTHREAD_ENABLE
  705. if (NULL != thread) {
  706. rt_thread_delete(thread->thread);
  707. pikaFree(thread, sizeof(pika_platform_thread_t));
  708. return;
  709. }
  710. #elif PIKA_ZEUSOS_ENABLE
  711. if (NULL != thread) {
  712. zos_task_destroy(thread->thread);
  713. pikaFree(thread, sizeof(pika_platform_thread_t));
  714. return;
  715. }
  716. #else
  717. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  718. #endif
  719. }
  720. PIKA_WEAK void pika_platform_thread_exit(pika_platform_thread_t* thread) {
  721. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  722. pika_platform_thread_destroy(thread);
  723. #elif PIKA_FREERTOS_ENABLE
  724. // vTaskDelete(NULL); // test on esp32c3
  725. if (NULL == thread) {
  726. vTaskDelete(NULL);
  727. return;
  728. }
  729. vTaskDelete(thread->thread);
  730. return;
  731. #elif PIKA_RTTHREAD_ENABLE
  732. if (NULL == thread) {
  733. rt_thread_delete(NULL);
  734. return;
  735. }
  736. rt_thread_delete(thread->thread);
  737. return;
  738. #elif PIKA_ZEUSOS_ENABLE
  739. if (NULL == thread) {
  740. zos_task_exit();
  741. return;
  742. }
  743. zos_task_destroy(thread->thread);
  744. return;
  745. #else
  746. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  747. #endif
  748. }
  749. PIKA_WEAK int pika_platform_thread_mutex_init(pika_platform_thread_mutex_t* m) {
  750. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  751. return pthread_mutex_init(&(m->mutex), NULL);
  752. #elif PIKA_FREERTOS_ENABLE
  753. m->mutex = xSemaphoreCreateMutex();
  754. return 0;
  755. #elif PIKA_RTTHREAD_ENABLE
  756. m->mutex = rt_mutex_create("pika_platform_mutex", RT_IPC_FLAG_PRIO);
  757. return 0;
  758. #elif PIKA_ZEUSOS_ENABLE
  759. static int mutex_count = 0;
  760. char mutex_name[ZOS_NAME_MAX + 1] = {0};
  761. zos_sprintf(mutex_name, "pika_mutex%d", mutex_count++);
  762. m->mutex = zos_mutex_create(mutex_name, ZOS_FALSE);
  763. return 0;
  764. #else
  765. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  766. return -1;
  767. #endif
  768. }
  769. PIKA_WEAK int pika_platform_thread_mutex_lock(pika_platform_thread_mutex_t* m) {
  770. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  771. return pthread_mutex_lock(&(m->mutex));
  772. #elif PIKA_FREERTOS_ENABLE
  773. if (pdTRUE == xSemaphoreTake(m->mutex, portMAX_DELAY)) {
  774. return 0;
  775. }
  776. return -1;
  777. #elif PIKA_RTTHREAD_ENABLE
  778. return rt_mutex_take((m->mutex), RT_WAITING_FOREVER);
  779. #elif PIKA_ZEUSOS_ENABLE
  780. return zos_mutex_lock(m->mutex, ZOS_WAIT_FOREVER);
  781. #else
  782. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  783. return -1;
  784. #endif
  785. }
  786. PIKA_WEAK int pika_platform_thread_mutex_trylock(
  787. pika_platform_thread_mutex_t* m) {
  788. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  789. return pthread_mutex_trylock(&(m->mutex));
  790. #elif PIKA_FREERTOS_ENABLE
  791. if (pdTRUE == xSemaphoreTake(m->mutex, 0)) {
  792. return 0;
  793. }
  794. return -1;
  795. #elif PIKA_RTTHREAD_ENABLE
  796. return rt_mutex_take((m->mutex), 0);
  797. #elif PIKA_ZEUSOS_ENABLE
  798. return zos_mutex_lock(m->mutex, 0);
  799. #else
  800. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  801. return -1;
  802. #endif
  803. }
  804. PIKA_WEAK int pika_platform_thread_mutex_unlock(
  805. pika_platform_thread_mutex_t* m) {
  806. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  807. return pthread_mutex_unlock(&(m->mutex));
  808. #elif PIKA_FREERTOS_ENABLE
  809. return xSemaphoreGive(m->mutex);
  810. #elif PIKA_RTTHREAD_ENABLE
  811. return rt_mutex_release((m->mutex));
  812. #elif PIKA_ZEUSOS_ENABLE
  813. return zos_mutex_unlock(m->mutex);
  814. #else
  815. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  816. return -1;
  817. #endif
  818. }
  819. PIKA_WEAK int pika_platform_thread_mutex_destroy(
  820. pika_platform_thread_mutex_t* m) {
  821. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  822. return pthread_mutex_destroy(&(m->mutex));
  823. #elif PIKA_FREERTOS_ENABLE
  824. vSemaphoreDelete(m->mutex);
  825. return 0;
  826. #elif PIKA_RTTHREAD_ENABLE
  827. return rt_mutex_delete((m->mutex));
  828. #elif PIKA_ZEUSOS_ENABLE
  829. return zos_mutex_destroy(m->mutex);
  830. #else
  831. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  832. return -1;
  833. #endif
  834. }
  835. int pika_thread_recursive_mutex_init(pika_thread_recursive_mutex_t* m) {
  836. int ret = 0;
  837. #if PIKA_ZEUSOS_ENABLE
  838. static int mutex_count = 0;
  839. char mutex_name[ZOS_NAME_MAX + 1] = {0};
  840. zos_sprintf(mutex_name, "pika_rec_mutex%d", mutex_count++);
  841. m->mutex.mutex = zos_mutex_create(mutex_name, ZOS_TRUE);
  842. if (m->mutex.mutex == ZOS_NULL) {
  843. return -1;
  844. }
  845. #else
  846. ret = pika_platform_thread_mutex_init(&m->mutex);
  847. if (ret != 0) {
  848. return ret;
  849. }
  850. #endif
  851. m->owner = 0;
  852. m->lock_times = 0;
  853. return 0;
  854. }
  855. int pika_thread_recursive_mutex_lock(pika_thread_recursive_mutex_t* m) {
  856. uint64_t self = pika_platform_thread_self();
  857. if (m->owner == self) {
  858. m->lock_times++;
  859. return 0;
  860. }
  861. int ret = 0;
  862. #if PIKA_ZEUSOS_ENABLE
  863. ret = zos_mutex_recursive_lock(m->mutex.mutex, ZOS_WAIT_FOREVER);
  864. #else
  865. ret = pika_platform_thread_mutex_lock(&m->mutex);
  866. #endif
  867. if (ret != 0) {
  868. return ret;
  869. }
  870. m->owner = self;
  871. m->lock_times = 1;
  872. return 0;
  873. }
  874. int pika_thread_recursive_mutex_trylock(pika_thread_recursive_mutex_t* m) {
  875. uint64_t self = pika_platform_thread_self();
  876. if (m->owner == self) {
  877. m->lock_times++;
  878. return 0;
  879. }
  880. int ret = 0;
  881. #if PIKA_ZEUSOS_ENABLE
  882. ret = zos_mutex_recursive_lock(m->mutex.mutex, 0);
  883. #else
  884. ret = pika_platform_thread_mutex_trylock(&m->mutex);
  885. #endif
  886. if (ret != 0) {
  887. return ret;
  888. }
  889. m->owner = self;
  890. m->lock_times = 1;
  891. return 0;
  892. }
  893. int pika_thread_recursive_mutex_unlock(pika_thread_recursive_mutex_t* m) {
  894. if (m->owner != pika_platform_thread_self()) {
  895. return -1;
  896. }
  897. m->lock_times--;
  898. if (m->lock_times == 0) {
  899. m->owner = 0;
  900. #if PIKA_ZEUSOS_ENABLE
  901. return zos_mutex_recursive_unlock(m->mutex.mutex);
  902. #else
  903. return pika_platform_thread_mutex_unlock(&m->mutex);
  904. #endif
  905. }
  906. return 0;
  907. }
  908. int pika_thread_recursive_mutex_destroy(pika_thread_recursive_mutex_t* m) {
  909. #if PIKA_ZEUSOS_ENABLE
  910. return zos_mutex_destroy(m->mutex.mutex);
  911. #else
  912. return pika_platform_thread_mutex_destroy(&m->mutex);
  913. #endif
  914. }
  915. PIKA_WEAK void pika_platform_thread_timer_init(pika_platform_timer_t* timer) {
  916. #ifdef __linux
  917. timer->time = (struct timeval){0, 0};
  918. #elif PIKA_WIN_PTHREAD_ENABLE
  919. timer->platform_data = pikaMalloc(sizeof(struct timeval));
  920. *((struct timeval*)(timer->platform_data)) = (struct timeval){0, 0};
  921. #elif PIKA_FREERTOS_ENABLE
  922. timer->time = 0;
  923. #elif PIKA_RTTHREAD_ENABLE
  924. timer->time = 0;
  925. #elif PIKA_ZEUSOS_ENABLE
  926. timer->time = 0;
  927. #else
  928. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  929. #endif
  930. }
  931. PIKA_WEAK void pika_platform_thread_timer_cutdown(pika_platform_timer_t* timer,
  932. unsigned int timeout) {
  933. #ifdef __linux
  934. struct timeval now;
  935. gettimeofday(&now, NULL);
  936. struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
  937. timeradd(&now, &interval, &timer->time);
  938. #elif PIKA_WIN_PTHREAD_ENABLE
  939. struct timeval* timer_temp = (struct timeval*)timer->platform_data;
  940. struct timeval now;
  941. gettimeofday(&now, NULL);
  942. struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
  943. timeradd(&now, &interval, timer_temp);
  944. #elif PIKA_FREERTOS_ENABLE
  945. timer->time = platform_uptime_ms();
  946. timer->time += timeout;
  947. #elif PIKA_RTTHREAD_ENABLE
  948. rt_uint32_t tick = rt_tick_get() * 1000;
  949. timer->time =
  950. (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND);
  951. timer->time += timeout;
  952. #elif PIKA_ZEUSOS_ENABLE
  953. zos_uint32_t tick = zos_tick_get() * 1000;
  954. timer->time =
  955. (uint32_t)((tick + ZOS_TICK_PER_SECOND - 1) / ZOS_TICK_PER_SECOND);
  956. timer->time += timeout;
  957. #else
  958. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  959. #endif
  960. }
  961. PIKA_WEAK char pika_platform_thread_timer_is_expired(
  962. pika_platform_timer_t* timer) {
  963. #ifdef __linux
  964. struct timeval now, res;
  965. gettimeofday(&now, NULL);
  966. timersub(&timer->time, &now, &res);
  967. return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
  968. #elif PIKA_WIN_PTHREAD_ENABLE
  969. struct timeval* timer_temp = (struct timeval*)timer->platform_data;
  970. struct timeval now, res;
  971. gettimeofday(&now, NULL);
  972. timersub(timer_temp, &now, &res);
  973. return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
  974. #elif PIKA_FREERTOS_ENABLE
  975. return platform_uptime_ms() > timer->time ? 1 : 0;
  976. #elif PIKA_RTTHREAD_ENABLE
  977. uint32_t tick = rt_tick_get() * 1000;
  978. uint32_t time =
  979. (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND);
  980. return time > timer->time ? 1 : 0;
  981. #elif PIKA_ZEUSOS_ENABLE
  982. uint32_t tick = zos_tick_get() * 1000;
  983. uint32_t time =
  984. (uint32_t)((tick + ZOS_TICK_PER_SECOND - 1) / ZOS_TICK_PER_SECOND);
  985. return time > timer->time ? 1 : 0;
  986. #else
  987. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  988. return 1;
  989. #endif
  990. }
  991. PIKA_WEAK int pika_platform_thread_timer_remain(pika_platform_timer_t* timer) {
  992. #ifdef __linux
  993. struct timeval now, res;
  994. gettimeofday(&now, NULL);
  995. timersub(&timer->time, &now, &res);
  996. return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
  997. #elif PIKA_WIN_PTHREAD_ENABLE
  998. struct timeval* timer_temp = (struct timeval*)timer->platform_data;
  999. struct timeval now, res;
  1000. gettimeofday(&now, NULL);
  1001. timersub(timer_temp, &now, &res);
  1002. return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
  1003. #elif PIKA_FREERTOS_ENABLE
  1004. uint32_t now;
  1005. now = platform_uptime_ms();
  1006. if (timer->time <= now) {
  1007. return 0;
  1008. }
  1009. return timer->time - now;
  1010. #elif PIKA_RTTHREAD_ENABLE
  1011. uint32_t now;
  1012. uint32_t tick = rt_tick_get() * 1000;
  1013. now = (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND);
  1014. if (timer->time <= now) {
  1015. return 0;
  1016. }
  1017. return timer->time - now;
  1018. #elif PIKA_ZEUSOS_ENABLE
  1019. uint32_t now;
  1020. uint32_t tick = zos_tick_get() * 1000;
  1021. now = (uint32_t)((tick + ZOS_TICK_PER_SECOND - 1) / ZOS_TICK_PER_SECOND);
  1022. if (timer->time <= now) {
  1023. return 0;
  1024. }
  1025. return timer->time - now;
  1026. #else
  1027. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  1028. return -1;
  1029. #endif
  1030. }
  1031. PIKA_WEAK unsigned long pika_platform_thread_timer_now(void) {
  1032. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  1033. return (unsigned long)time(NULL);
  1034. #elif PIKA_FREERTOS_ENABLE
  1035. return (unsigned long)platform_uptime_ms();
  1036. #elif PIKA_RTTHREAD_ENABLE
  1037. uint32_t tick = rt_tick_get() * 1000;
  1038. return (unsigned long)((tick + RT_TICK_PER_SECOND - 1) /
  1039. RT_TICK_PER_SECOND);
  1040. #elif PIKA_ZEUSOS_ENABLE
  1041. uint32_t tick = zos_tick_get() * 1000;
  1042. return (unsigned long)((tick + ZOS_TICK_PER_SECOND - 1) /
  1043. ZOS_TICK_PER_SECOND);
  1044. #else
  1045. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  1046. return 1;
  1047. #endif
  1048. }
  1049. PIKA_WEAK void pika_platform_thread_timer_usleep(unsigned long usec) {
  1050. #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
  1051. usleep(usec);
  1052. #elif PIKA_FREERTOS_ENABLE
  1053. TickType_t tick = 1;
  1054. if (usec != 0) {
  1055. tick = usec / portTICK_PERIOD_MS;
  1056. if (tick == 0)
  1057. tick = 1;
  1058. }
  1059. vTaskDelay(tick);
  1060. #elif PIKA_RTTHREAD_ENABLE
  1061. uint32_t tick = 1;
  1062. if (usec != 0) {
  1063. tick = usec / RT_TICK_PER_SECOND;
  1064. if (tick == 0)
  1065. tick = 1;
  1066. }
  1067. rt_thread_mdelay(tick);
  1068. #elif PIKA_ZEUSOS_ENABLE
  1069. zos_tick_t tick = 1;
  1070. if (usec != 0) {
  1071. tick = usec / ZOS_TICK_PER_SECOND;
  1072. if (tick == 0)
  1073. tick = 1;
  1074. }
  1075. zos_task_tsleep(tick);
  1076. #else
  1077. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  1078. #endif
  1079. }
  1080. /* system support */
  1081. PIKA_WEAK void pika_platform_reboot(void) {
  1082. #if __linux
  1083. pika_platform_printf("reboot\n");
  1084. #else
  1085. WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
  1086. #endif
  1087. }
  1088. /* hook */
  1089. PIKA_WEAK void pika_platform_error_handle() {
  1090. return;
  1091. }
  1092. PIKA_WEAK void pika_platform_panic_handle() {
  1093. while (1) {
  1094. };
  1095. }
  1096. PIKA_WEAK void pika_hook_instruct(void) {
  1097. return;
  1098. }
  1099. PIKA_WEAK PIKA_BOOL pika_hook_arg_cache_filter(void* self) {
  1100. return PIKA_TRUE;
  1101. }
  1102. PIKA_WEAK void pika_platform_abort_handler(void) {
  1103. return;
  1104. }
  1105. PIKA_WEAK void pika_thread_idle_hook(void) {
  1106. return;
  1107. }
  1108. #endif