aos_posix.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (C) 2015-2017 Alibaba Group Holding Limited
  3. */
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <sys/time.h>
  9. #include <sys/prctl.h>
  10. #include <pthread.h>
  11. #include <errno.h>
  12. #undef WITH_LWIP
  13. #undef WITH_SAL
  14. #include <poll.h>
  15. #include <aos/kernel.h>
  16. #include <ulog/ulog.h>
  17. #define TAG "AOS"
  18. void aos_reboot(void)
  19. {
  20. exit(0);
  21. }
  22. int aos_get_hz(void)
  23. {
  24. return 100;
  25. }
  26. const char *aos_version_get(void)
  27. {
  28. return "aos-linux-xxx";
  29. }
  30. struct targ {
  31. const char *name;
  32. void (*fn)(void *);
  33. void *arg;
  34. };
  35. static void *dfl_entry(void *arg)
  36. {
  37. struct targ *targ = arg;
  38. void (*fn)(void *) = targ->fn;
  39. void *farg = targ->arg;
  40. prctl(PR_SET_NAME, (unsigned long)targ->name, 0, 0, 0);
  41. free(targ);
  42. fn(farg);
  43. return 0;
  44. }
  45. int aos_task_new(const char *name, void (*fn)(void *), void *arg,
  46. int stack_size)
  47. {
  48. int ret;
  49. pthread_t th;
  50. struct targ *targ = malloc(sizeof(*targ));
  51. targ->name = strdup(name);
  52. targ->fn = fn;
  53. targ->arg = arg;
  54. ret = pthread_create(&th, NULL, dfl_entry, targ);
  55. if (ret == 0) ret = pthread_detach(th);
  56. return ret;
  57. }
  58. int aos_task_new_ext(aos_task_t *task, const char *name, void (*fn)(void *), void *arg,
  59. int stack_size, int prio)
  60. {
  61. return aos_task_new(name, fn, arg, stack_size);
  62. }
  63. void aos_task_exit(int code)
  64. {
  65. int ret;
  66. pthread_exit(&ret);
  67. }
  68. const char *aos_task_name(void)
  69. {
  70. static char name[16];
  71. prctl(PR_GET_NAME, (unsigned long)name, 0, 0, 0);
  72. return name;
  73. }
  74. int aos_task_key_create(aos_task_key_t *key)
  75. {
  76. return pthread_key_create(key, NULL);
  77. }
  78. void aos_task_key_delete(aos_task_key_t key)
  79. {
  80. pthread_key_delete(key);
  81. }
  82. int aos_task_setspecific(aos_task_key_t key, void *vp)
  83. {
  84. return pthread_setspecific(key, vp);
  85. }
  86. void *aos_task_getspecific(aos_task_key_t key)
  87. {
  88. return pthread_getspecific(key);
  89. }
  90. int aos_mutex_new(aos_mutex_t *mutex)
  91. {
  92. pthread_mutex_t *mtx = malloc(sizeof(*mtx));
  93. pthread_mutexattr_t attr;
  94. pthread_mutexattr_init(&attr);
  95. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
  96. pthread_mutex_init(mtx, &attr);
  97. mutex->hdl = mtx;
  98. return 0;
  99. }
  100. void aos_mutex_free(aos_mutex_t *mutex)
  101. {
  102. pthread_mutex_destroy(mutex->hdl);
  103. free(mutex->hdl);
  104. }
  105. int aos_mutex_lock(aos_mutex_t *mutex, unsigned int timeout)
  106. {
  107. if (!mutex)
  108. return -EINVAL;
  109. if (timeout == AOS_WAIT_FOREVER) {
  110. pthread_mutex_lock(mutex->hdl);
  111. } else {
  112. struct timespec abs_timeout;
  113. abs_timeout.tv_sec = timeout / 1000;
  114. abs_timeout.tv_nsec = timeout % 1000 * 1000000;
  115. return pthread_mutex_timedlock(mutex->hdl, &abs_timeout);
  116. }
  117. return 0;
  118. }
  119. int aos_mutex_unlock(aos_mutex_t *mutex)
  120. {
  121. if (mutex) {
  122. pthread_mutex_unlock(mutex->hdl);
  123. }
  124. return 0;
  125. }
  126. int aos_mutex_is_valid(aos_mutex_t *mutex)
  127. {
  128. return mutex->hdl != NULL;
  129. }
  130. #include <semaphore.h>
  131. int aos_sem_new(aos_sem_t *sem, int count)
  132. {
  133. sem_t *s = malloc(sizeof(*s));
  134. sem_init(s, 0, count);
  135. sem->hdl = s;
  136. return 0;
  137. }
  138. void aos_sem_free(aos_sem_t *sem)
  139. {
  140. if (sem == NULL) {
  141. return;
  142. }
  143. sem_destroy(sem->hdl);
  144. free(sem->hdl);
  145. }
  146. int aos_sem_wait(aos_sem_t *sem, unsigned int timeout)
  147. {
  148. int sec;
  149. int nsec;
  150. if (sem == NULL) {
  151. return -EINVAL;
  152. }
  153. if (timeout == AOS_WAIT_FOREVER) {
  154. return sem_wait(sem->hdl);
  155. } else if (timeout == 0) {
  156. return sem_trywait(sem->hdl);
  157. }
  158. struct timespec ts;
  159. clock_gettime(CLOCK_REALTIME, &ts);
  160. sec = timeout / 1000;
  161. nsec = (timeout % 1000) * 1000;
  162. ts.tv_nsec += nsec;
  163. sec += (ts.tv_nsec / 1000000000);
  164. ts.tv_nsec %= 1000000000;
  165. ts.tv_sec += sec;
  166. return sem_timedwait(sem->hdl, &ts);
  167. }
  168. void aos_sem_signal(aos_sem_t *sem)
  169. {
  170. if (sem == NULL) {
  171. return;
  172. }
  173. sem_post(sem->hdl);
  174. }
  175. int aos_sem_is_valid(aos_sem_t *sem)
  176. {
  177. return sem && sem->hdl != NULL;
  178. }
  179. void aos_sem_signal_all(aos_sem_t *sem)
  180. {
  181. sem_post(sem->hdl);
  182. }
  183. struct queue {
  184. int fds[2];
  185. void *buf;
  186. int size;
  187. int msg_size;
  188. };
  189. int aos_queue_new(aos_queue_t *queue, void *buf, unsigned int size, int max_msg)
  190. {
  191. struct queue *q = malloc(sizeof(*q));
  192. pipe(q->fds);
  193. q->buf = buf;
  194. q->size = size;
  195. q->msg_size = max_msg;
  196. queue->hdl = q;
  197. return 0;
  198. }
  199. void aos_queue_free(aos_queue_t *queue)
  200. {
  201. struct queue *q = queue->hdl;
  202. close(q->fds[0]);
  203. close(q->fds[1]);
  204. free(q);
  205. }
  206. int aos_queue_send(aos_queue_t *queue, void *msg, unsigned int size)
  207. {
  208. struct queue *q = queue->hdl;
  209. write(q->fds[1], msg, size);
  210. return 0;
  211. }
  212. int aos_queue_recv(aos_queue_t *queue, unsigned int ms, void *msg,
  213. unsigned int *size)
  214. {
  215. struct queue *q = queue->hdl;
  216. struct pollfd rfd = {
  217. .fd = q->fds[0],
  218. .events = POLLIN,
  219. };
  220. poll(&rfd, 1, ms);
  221. if (rfd.revents & POLLIN) {
  222. int len = read(q->fds[0], msg, q->msg_size);
  223. *size = len;
  224. return len < 0 ? -1 : 0;
  225. }
  226. return -1;
  227. }
  228. int aos_queue_is_valid(aos_queue_t *queue)
  229. {
  230. return queue->hdl != NULL;
  231. }
  232. void *aos_queue_buf_ptr(aos_queue_t *queue)
  233. {
  234. struct queue *q = queue->hdl;
  235. return q->buf;
  236. }
  237. struct work {
  238. void (*fn)(void *);
  239. void *arg;
  240. int dly;
  241. };
  242. int aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly)
  243. {
  244. struct work *w = malloc(sizeof(*w));
  245. w->fn = fn;
  246. w->arg = arg;
  247. w->dly = dly;
  248. work->hdl = w;
  249. return 0;
  250. }
  251. void aos_work_destroy(aos_work_t *work)
  252. {
  253. free(work->hdl);
  254. }
  255. int aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work)
  256. {
  257. return aos_work_sched(work);
  258. }
  259. static void worker_entry(void *arg)
  260. {
  261. struct work *w = arg;
  262. if (w->dly) {
  263. usleep(w->dly * 1000);
  264. }
  265. w->fn(w->arg);
  266. }
  267. int aos_work_sched(aos_work_t *work)
  268. {
  269. struct work *w = work->hdl;
  270. return aos_task_new("worker", worker_entry, w, 8192);
  271. }
  272. int aos_work_cancel(aos_work_t *work)
  273. {
  274. return -1;
  275. }
  276. void *aos_zalloc(unsigned int size)
  277. {
  278. return calloc(size, 1);
  279. }
  280. void *aos_zalloc_check(unsigned int size)
  281. {
  282. return calloc(size, 1);
  283. }
  284. void *aos_malloc(unsigned int size)
  285. {
  286. return malloc(size);
  287. }
  288. void *aos_calloc(unsigned int size, int num)
  289. {
  290. void *ptr = malloc(size * num);
  291. if (ptr) {
  292. memset (ptr, 0, size * num);
  293. }
  294. return ptr;
  295. }
  296. void *aos_realloc(void *mem, unsigned int size)
  297. {
  298. return realloc(mem, size);
  299. }
  300. void aos_alloc_trace(void *addr, size_t allocator)
  301. {
  302. }
  303. void aos_free(void *mem)
  304. {
  305. free(mem);
  306. }
  307. long long aos_now()
  308. {
  309. struct timespec ts;
  310. if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
  311. LOGE(TAG, "%s unable to get current time: %s",
  312. __func__, strerror(errno));
  313. return 0;
  314. }
  315. return (ts.tv_sec * 1000000000LL) + (ts.tv_nsec);
  316. }
  317. long long aos_now_ms()
  318. {
  319. struct timespec ts;
  320. if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
  321. LOGE(TAG, "%s unable to get current time: %s",
  322. __func__, strerror(errno));
  323. return 0;
  324. }
  325. return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
  326. }
  327. void aos_msleep(int ms)
  328. {
  329. usleep(ms * 1000);
  330. }
  331. void aos_init(void)
  332. {
  333. }
  334. void aos_start(void)
  335. {
  336. while (1) {
  337. usleep(1000 * 1000 * 100);
  338. }
  339. }
  340. #include <stdio.h>
  341. #include <sys/types.h>
  342. #include <dirent.h>
  343. void dumpsys_task_func(void)
  344. {
  345. DIR *proc = opendir("/proc/self/task");
  346. while (1) {
  347. struct dirent *ent = readdir(proc);
  348. if (!ent) {
  349. break;
  350. }
  351. if (ent->d_name[0] == '.') {
  352. continue;
  353. }
  354. char fn[128];
  355. snprintf(fn, sizeof fn, "/proc/self/task/%s/comm", ent->d_name);
  356. FILE *fp = fopen(fn, "r");
  357. if (!fp) {
  358. continue;
  359. }
  360. bzero(fn, sizeof fn);
  361. fread(fn, sizeof(fn) - 1, 1, fp);
  362. fclose(fp);
  363. printf("%8s - %s", ent->d_name, fn);
  364. }
  365. closedir(proc);
  366. }
  367. int aos_event_get(aos_event_t *event, unsigned int flags, unsigned char opt,
  368. unsigned int *actl_flags, unsigned int timeout)
  369. {
  370. int sec;
  371. int nsec;
  372. if (event == NULL) {
  373. return -EINVAL;
  374. }
  375. if (timeout == AOS_WAIT_FOREVER) {
  376. return sem_wait(event->hdl);
  377. } else if (timeout == 0) {
  378. return sem_trywait(event->hdl);
  379. }
  380. struct timespec ts;
  381. clock_gettime(CLOCK_REALTIME, &ts);
  382. sec = timeout / 1000;
  383. nsec = (timeout % 1000) * 1000;
  384. ts.tv_nsec += nsec;
  385. sec += (ts.tv_nsec / 1000000000);
  386. ts.tv_nsec %= 1000000000;
  387. ts.tv_sec += sec;
  388. return sem_timedwait(event->hdl, &ts);
  389. }
  390. int aos_event_set(aos_event_t *event, unsigned int flags, unsigned char opt)
  391. {
  392. if (event == NULL) {
  393. return -EINVAL;
  394. }
  395. sem_post(event->hdl);
  396. return 0;
  397. }
  398. int aos_event_is_valid(aos_event_t *event)
  399. {
  400. return event && event->hdl != NULL;
  401. }
  402. void aos_event_free(aos_event_t *event)
  403. {
  404. if (event == NULL) {
  405. return;
  406. }
  407. sem_destroy(event->hdl);
  408. free(event->hdl);
  409. }
  410. int aos_event_new(aos_event_t *event, unsigned int flags)
  411. {
  412. sem_t *s = malloc(sizeof(*s));
  413. sem_init(s, 0, 0);
  414. event->hdl = s;
  415. return 0;
  416. }