• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <sys/prctl.h>
21 #include <sys/socket.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include "sample_media_ai.h"
25 #include "posix_help.h"
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif /* End of #ifdef __cplusplus */
32 
33 #define SKPAIR_FDS 2
34 
35 /* SkPair create */
SkPairCreate(SkPair * chn)36 int SkPairCreate(SkPair* chn)
37 {
38     HI_ASSERT(chn);
39     int fds[SKPAIR_FDS];
40 
41     if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, fds) < 0) {
42         HI_ASSERT(0);
43     }
44     chn->in = fds[0];
45     chn->out = fds[1];
46     return 0;
47 }
48 
49 /* Read complete message */
FdReadMsg(int fd,HI_VOID * msgBuf,int msgSize)50 int FdReadMsg(int fd, HI_VOID* msgBuf, int msgSize)
51 {
52     HI_ASSERT(msgBuf && msgSize > 0);
53     static const int logPerCount = 1000; // Circulate 1000 times and log information once
54     int loopNum = 0;
55     int total = 0;
56     int ret;
57     while (total < msgSize) {
58         ret = read(fd, (char*)msgBuf + total, msgSize - total);
59         if (ret > 0) {
60             total += ret;
61         } else if (ret == 0) { // fd closed by peer
62             SAMPLE_PRT("read FAIL, for fd closed by peer\n");
63             return -1;
64         } else if (errno == EAGAIN || errno == EWOULDBLOCK) { // no data now
65             if (total == 0) { // When no data has been read before, return
66                 return 0;
67             }
68             if (++loopNum % logPerCount == 0) {
69                 SAMPLE_PRT("FdReadMsg wait %ds, ERR, CHECK it\n", loopNum / logPerCount);
70             }
71             usleep(HI_USLEEP_MS); // wait 1ms, and try again
72         } else {
73             return -1; // read error
74         }
75     }
76     return msgSize;
77 }
78 
79 /* Write complete message */
FdWriteMsg(int fd,const HI_VOID * msgData,int msgLen)80 int FdWriteMsg(int fd, const HI_VOID* msgData, int msgLen)
81 {
82     HI_ASSERT(msgData && msgLen > 0);
83     static const int logPerCount = 1000; // Circulate 1000 times and log information once
84     int loopNum = 0;
85     int total = 0;
86     int ret;
87 
88     while (total < msgLen) {
89         ret = write(fd, (const char*)msgData + total, msgLen - total);
90         if (ret > 0) {
91             total += ret;
92         } else if (ret == 0 || (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) { // no data now
93             if (++loopNum % logPerCount == 0) {
94                 SAMPLE_PRT("FdWriteMsg wait %ds, ERR, CHECK it\n", loopNum / logPerCount);
95             }
96             usleep(HI_USLEEP_MS); // wait 1ms, and try again
97         } else { // write error
98             HI_ASSERT(ret < 0);
99             SAMPLE_PRT("FdWriteMsg FAIL, err='%s'\n", strerror(errno));
100             return -1;
101         }
102     }
103     return msgLen;
104 }
105 
106 /* SkPair destroy */
SkPairDestroy(SkPair * chn)107 void SkPairDestroy(SkPair* chn)
108 {
109     HI_ASSERT(chn);
110 
111     if (chn->in >= 0) {
112         if (close(chn->in) < 0) {
113             HI_ASSERT(0);
114         }
115         chn->in = -1;
116     }
117     if (chn->out >= 0) {
118         if (close(chn->out) < 0) {
119             HI_ASSERT(0);
120         }
121         chn->out = -1;
122     }
123 }
124 
125 /* init recursive pmutex replaced PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
RecurMutexInit(pthread_mutex_t * mutex)126 void RecurMutexInit(pthread_mutex_t* mutex)
127 {
128     HI_ASSERT(mutex);
129     pthread_mutexattr_t attr;
130     int res;
131 
132     res = pthread_mutexattr_init(&attr);
133     HI_ASSERT(!res);
134 
135     res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
136     HI_ASSERT(!res);
137     res = pthread_mutex_init(mutex, &attr);
138     HI_ASSERT(!res);
139 
140     pthread_mutexattr_destroy(&attr);
141 }
142 
143 #ifdef __cplusplus
144 #if __cplusplus
145 }
146 #endif
147 #endif /* End of #ifdef __cplusplus */
148