cnm_task.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "vdi_osal.h"
  27. #include "cnm_app.h"
  28. #include "cnm_app_internal.h"
  29. CNMTask CNMTaskCreate(void)
  30. {
  31. CNMTaskContext* ctx = (CNMTaskContext*)osal_malloc(sizeof(CNMTaskContext));
  32. if (ctx == NULL) {
  33. VLOG(ERR, "%s:%d Failed to allocate a memory\n", __FUNCTION__, __LINE__);
  34. return NULL;
  35. }
  36. osal_memset((void*)ctx, 0x00, sizeof(CNMTaskContext));
  37. ctx->oneTimeRun = supportThread;
  38. return (CNMTask)ctx;
  39. }
  40. void CNMTaskDestroy(CNMTask task)
  41. {
  42. CNMTaskContext* ctx = (CNMTaskContext*)task;
  43. Uint32 i;
  44. if (task == NULL) {
  45. VLOG(WARN, "%s:%d HANDLE is NULL\n", __FUNCTION__, __LINE__);
  46. return;
  47. }
  48. for (i=0; i<ctx->numComponents; i++) {
  49. ComponentRelease(ctx->componentList[i]);
  50. }
  51. for (i=0; i<ctx->numComponents; i++) {
  52. ComponentDestroy(ctx->componentList[i], NULL);
  53. }
  54. osal_free(task);
  55. return;
  56. }
  57. BOOL CNMTaskAdd(CNMTask task, Component component)
  58. {
  59. CNMTaskContext* ctx = (CNMTaskContext*)task;
  60. Uint32 num = ctx->numComponents;
  61. if (ctx->numComponents == MAX_COMPONENTS_IN_TASK) {
  62. return FALSE;
  63. }
  64. ctx->componentList[num++] = (void*)component;
  65. ctx->numComponents = num;
  66. return TRUE;
  67. }
  68. BOOL CNMTaskRun(CNMTask task)
  69. {
  70. CNMTaskContext* ctx = (CNMTaskContext*)task;
  71. Uint32 i;
  72. ComponentImpl* com;
  73. BOOL terminate = FALSE;
  74. BOOL stopComponents = FALSE;
  75. if (ctx->componentsConnected == FALSE) {
  76. for (i=0; i<ctx->numComponents-1; i++) {
  77. Component from = (Component)ctx->componentList[i];
  78. Component to = (Component)ctx->componentList[i+1];
  79. if (ComponentSetupTunnel(from, to) == FALSE) {
  80. return FALSE;
  81. }
  82. }
  83. ctx->componentsConnected = TRUE;
  84. }
  85. while (terminate == FALSE) {
  86. terminate = TRUE;
  87. for (i=0; i<ctx->numComponents; i++) {
  88. if ((com=ctx->componentList[i]) == NULL) {
  89. ctx->stop = TRUE;
  90. break;
  91. }
  92. if (stopComponents == TRUE) {
  93. /* Failure! stop all components */
  94. ComponentStop(com);
  95. }
  96. else {
  97. if (com->terminate == FALSE) {
  98. if (ComponentExecute(com) == COMPONENT_STATE_TERMINATED) {
  99. stopComponents = (com->success == FALSE);
  100. terminate &= TRUE;
  101. }
  102. else {
  103. terminate &= FALSE;
  104. }
  105. }
  106. }
  107. }
  108. if (ctx->oneTimeRun == TRUE) break;
  109. osal_msleep(1); // For cross-debugging on non-thread environment.
  110. }
  111. ctx->terminate = (terminate || stopComponents);
  112. return TRUE;
  113. }
  114. CNMTaskWaitState CNMTaskWait(CNMTask task)
  115. {
  116. Uint32 i;
  117. Uint32 doneCount = 0;
  118. Int32 ret = CNM_TASK_ERROR;
  119. ComponentImpl* com;
  120. CNMTaskContext* ctx = (CNMTaskContext*)task;
  121. BOOL stopComponents = FALSE;
  122. for (i=0; i<ctx->numComponents; i++) {
  123. if ((com=(Component)ctx->componentList[i]) != NULL) {
  124. if (stopComponents == TRUE) {
  125. ComponentStop(com);
  126. ret = ComponentWait(com);
  127. }
  128. else {
  129. if ((ret=ComponentWait(com)) == 0) {
  130. stopComponents = (com->success == FALSE);
  131. }
  132. }
  133. }
  134. else {
  135. ret = 0; // A component might be terminated in the creating step.
  136. }
  137. if (ret == 0) doneCount++;
  138. }
  139. return (doneCount == ctx->numComponents) ? CNM_TASK_DONE : CNM_TASK_RUNNING;
  140. }
  141. BOOL CNMTaskStop(CNMTask task)
  142. {
  143. CNMTaskContext* ctx = (CNMTaskContext*)task;
  144. ComponentImpl* com;
  145. Uint32 i;
  146. BOOL success;
  147. if (task == NULL)
  148. return TRUE;
  149. for (i=0; i<ctx->numComponents; i++) {
  150. if (ctx->componentList[i]) ComponentStop(ctx->componentList[i]);
  151. }
  152. success = TRUE;
  153. for (i=0; i<ctx->numComponents; i++) {
  154. if ((com=(ComponentImpl*)ctx->componentList[i]) != NULL) {
  155. if (com->success == FALSE) {
  156. VLOG(WARN, "%s:%d <%s> returns FALSE\n", __FUNCTION__, __LINE__, com->name);
  157. }
  158. success &= com->success;
  159. }
  160. else success &= FALSE;
  161. }
  162. return success;
  163. }
  164. BOOL CNMTaskIsTerminated(CNMTask task)
  165. {
  166. CNMTaskContext* ctx = (CNMTaskContext*)task;
  167. return ctx->terminate;
  168. }