• 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 /*
36  * 创建Socketpair
37  * Socketpair create
38  */
SkPairCreate(SkPair * chn)39 int SkPairCreate(SkPair* chn)
40 {
41     HI_ASSERT(chn);
42     int fds[SKPAIR_FDS];
43 
44     if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, fds) < 0) {
45         HI_ASSERT(0);
46     }
47     chn->in = fds[0];
48     chn->out = fds[1];
49     return 0;
50 }
51 
52 /*
53  * 读完整的消息
54  * Read complete message
55  */
FdReadMsg(int fd,HI_VOID * msgBuf,int msgSize)56 int FdReadMsg(int fd, HI_VOID* msgBuf, int msgSize)
57 {
58     HI_ASSERT(msgBuf && msgSize > 0);
59     static const int logPerCount = 1000; // Circulate 1000 times and log information once
60     int loopNum = 0;
61     int total = 0;
62     int ret;
63     while (total < msgSize) {
64         ret = read(fd, (char*)msgBuf + total, msgSize - total);
65         if (ret > 0) {
66             total += ret;
67         } else if (ret == 0) { // fd closed by peer
68             SAMPLE_PRT("read FAIL, for fd closed by peer\n");
69             return -1;
70         } else if (errno == EAGAIN || errno == EWOULDBLOCK) { // no data now
71             if (total == 0) { // When no data has been read before, return
72                 return 0;
73             }
74             if (++loopNum % logPerCount == 0) {
75                 SAMPLE_PRT("FdReadMsg wait %ds, ERR, CHECK it\n", loopNum / logPerCount);
76             }
77             usleep(HI_USLEEP_MS); // wait 1ms, and try again
78         } else {
79             return -1; // read error
80         }
81     }
82     return msgSize;
83 }
84 
85 /*
86  * 写完整的消息
87  * Write complete message
88  */
FdWriteMsg(int fd,const HI_VOID * msgData,int msgLen)89 int FdWriteMsg(int fd, const HI_VOID* msgData, int msgLen)
90 {
91     HI_ASSERT(msgData && msgLen > 0);
92     static const int logPerCount = 1000; // Circulate 1000 times and log information once
93     int loopNum = 0;
94     int total = 0;
95     int ret;
96 
97     while (total < msgLen) {
98         ret = write(fd, (const char*)msgData + total, msgLen - total);
99         if (ret > 0) {
100             total += ret;
101         } else if (ret == 0 || (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) { // no data now
102             if (++loopNum % logPerCount == 0) {
103                 SAMPLE_PRT("FdWriteMsg wait %ds, ERR, CHECK it\n", loopNum / logPerCount);
104             }
105             usleep(HI_USLEEP_MS); // wait 1ms, and try again
106         } else { // write error
107             HI_ASSERT(ret < 0);
108             SAMPLE_PRT("FdWriteMsg FAIL, err='%s'\n", strerror(errno));
109             return -1;
110         }
111     }
112     return msgLen;
113 }
114 
115 /*
116  * 销毁socketpair
117  * Socketpair destroy
118  */
SkPairDestroy(SkPair * chn)119 void SkPairDestroy(SkPair* chn)
120 {
121     HI_ASSERT(chn);
122 
123     if (chn->in >= 0) {
124         if (close(chn->in) < 0) {
125             HI_ASSERT(0);
126         }
127         chn->in = -1;
128     }
129     if (chn->out >= 0) {
130         if (close(chn->out) < 0) {
131             HI_ASSERT(0);
132         }
133         chn->out = -1;
134     }
135 }
136 
137 /*
138  * 初始化recursive pmutex
139  * Init recursive pmutex
140  */
RecurMutexInit(pthread_mutex_t * mutex)141 void RecurMutexInit(pthread_mutex_t* mutex)
142 {
143     HI_ASSERT(mutex);
144     pthread_mutexattr_t attr;
145     int res;
146 
147     res = pthread_mutexattr_init(&attr);
148     HI_ASSERT(!res);
149 
150     res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
151     HI_ASSERT(!res);
152     res = pthread_mutex_init(mutex, &attr);
153     HI_ASSERT(!res);
154 
155     pthread_mutexattr_destroy(&attr);
156 }
157 
158 #ifdef __cplusplus
159 #if __cplusplus
160 }
161 #endif
162 #endif /* End of #ifdef __cplusplus */
163