thread.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "os/thread.h"
  19. #include <pthread.h>
  20. #include <stdio.h>
  21. /**
  22. * Defines a helper function that adapts function pointer provided by caller to
  23. * the type required by pthread_create.
  24. *
  25. * @param toRun thread to run
  26. * @return void* result of running thread (always NULL)
  27. */
  28. static void* runThread(void *toRun) {
  29. const thread *t = toRun;
  30. t->start(t->arg);
  31. return NULL;
  32. }
  33. int threadCreate(thread *t) {
  34. int ret;
  35. ret = pthread_create(&t->id, NULL, runThread, t);
  36. if (ret) {
  37. fprintf(stderr, "threadCreate: pthread_create failed with error %d\n", ret);
  38. }
  39. return ret;
  40. }
  41. int threadJoin(const thread *t) {
  42. int ret = pthread_join(t->id, NULL);
  43. if (ret) {
  44. fprintf(stderr, "threadJoin: pthread_join failed with error %d\n", ret);
  45. }
  46. return ret;
  47. }