|
17 | 17 | * USA |
18 | 18 | * |
19 | 19 | */ |
| 20 | +#include "stream.h" |
| 21 | +#include "adapter.h" |
| 22 | +#include "api/symbols.h" |
| 23 | +#include "api/variables.h" |
| 24 | +#include "dvb.h" |
| 25 | +#include "minisatip.h" |
| 26 | +#include "pmt.h" |
| 27 | +#include "socketworks.h" |
20 | 28 | #include <arpa/inet.h> |
21 | 29 | #include <errno.h> |
22 | 30 | #include <fcntl.h> |
|
35 | 43 | #include <sys/types.h> |
36 | 44 | #include <time.h> |
37 | 45 | #include <unistd.h> |
38 | | - |
39 | | -#include "adapter.h" |
40 | | -#include "api/symbols.h" |
41 | | -#include "api/variables.h" |
42 | | -#include "dvb.h" |
43 | | -#include "minisatip.h" |
44 | | -#include "pmt.h" |
45 | | -#include "socketworks.h" |
46 | | -#include "stream.h" |
| 46 | +#include <vector> |
47 | 47 |
|
48 | 48 | #include "utils/ticks.h" |
49 | 49 |
|
@@ -224,6 +224,7 @@ streams *setup_stream(char *str, sockets *s) { |
224 | 224 | int ad = sid->adapter; |
225 | 225 | if (!strstr(tmp_str, "addpids") && !strstr(tmp_str, "delpids")) { |
226 | 226 | close_adapter_for_stream(sid->sid, ad, 0); |
| 227 | + sid->adapter = -1; |
227 | 228 | } |
228 | 229 | } |
229 | 230 |
|
@@ -991,7 +992,6 @@ int process_packets_for_stream(streams *sid, adapter *ad) { |
991 | 992 | flush_stream(sid, iov, iiov, rtime); |
992 | 993 | return 0; |
993 | 994 | } |
994 | | - |
995 | 995 | int process_dmx(sockets *s) { |
996 | 996 | int i; |
997 | 997 | adapter *ad; |
@@ -1039,13 +1039,33 @@ int process_dmx(sockets *s) { |
1039 | 1039 | return 0; |
1040 | 1040 | } |
1041 | 1041 |
|
| 1042 | +std::vector<SMutex *> lock_streams_for_adapter(int aid) { |
| 1043 | + streams *sid; |
| 1044 | + std::vector<SMutex *> locks; |
| 1045 | + for (int i = 0; i < MAX_STREAMS; i++) |
| 1046 | + if ((sid = get_sid_nw(i)) && sid->adapter == aid) { |
| 1047 | + mutex_lock(&sid->mutex); |
| 1048 | + if (!sid->enabled) { |
| 1049 | + mutex_unlock(&sid->mutex); |
| 1050 | + continue; |
| 1051 | + } |
| 1052 | + locks.push_back(&sid->mutex); |
| 1053 | + } |
| 1054 | + return locks; |
| 1055 | +} |
| 1056 | +void unlock_streams_for_adapter(std::vector<SMutex *> locks) { |
| 1057 | + int i = 0; |
| 1058 | + for (i = locks.size() - 1; i >= 0; i--) |
| 1059 | + mutex_unlock(locks[i]); |
| 1060 | +} |
| 1061 | + |
1042 | 1062 | // lock order: socket -> stream -> adapter |
1043 | 1063 | // after stream or adapter, avoid locking socket |
1044 | 1064 |
|
1045 | 1065 | int read_dmx(sockets *s) { |
1046 | 1066 | static int cnt; |
1047 | 1067 | adapter *ad; |
1048 | | - int send = 0, force_send = 0, ls, lse; |
| 1068 | + int send = 0, force_send = 0; |
1049 | 1069 | int threshold = opts.udp_threshold; |
1050 | 1070 | int64_t rtime = getTick(); |
1051 | 1071 |
|
@@ -1112,13 +1132,11 @@ int read_dmx(sockets *s) { |
1112 | 1132 | return 0; |
1113 | 1133 |
|
1114 | 1134 | ad->flush = 0; |
1115 | | - ls = lock_streams_for_adapter(ad->id); |
| 1135 | + auto locks = lock_streams_for_adapter(ad->id); |
1116 | 1136 | mutex_lock(&ad->mutex); |
1117 | 1137 | process_dmx(s); |
1118 | 1138 | mutex_unlock(&ad->mutex); |
1119 | | - lse = unlock_streams_for_adapter(ad->id); |
1120 | | - if (ls != lse) |
1121 | | - LOG("leak detected %d %d!!! ", ls, lse); |
| 1139 | + unlock_streams_for_adapter(locks); |
1122 | 1140 | return 0; |
1123 | 1141 | } |
1124 | 1142 | #undef DEFAULT_LOG |
@@ -1220,28 +1238,6 @@ void dump_streams() { |
1220 | 1238 | get_stream_rport(sid->sid)); |
1221 | 1239 | } |
1222 | 1240 |
|
1223 | | -int lock_streams_for_adapter(int aid) { |
1224 | | - streams *sid; |
1225 | | - int i = 0, ls = 0; |
1226 | | - for (i = 0; i < MAX_STREAMS; i++) |
1227 | | - if ((sid = get_sid_nw(i)) && sid->adapter == aid) { |
1228 | | - mutex_lock(&sid->mutex); |
1229 | | - ls++; |
1230 | | - } |
1231 | | - return ls; |
1232 | | -} |
1233 | | - |
1234 | | -int unlock_streams_for_adapter(int aid) { |
1235 | | - streams *sid; |
1236 | | - int i = 0, ls = 0; |
1237 | | - for (i = MAX_STREAMS - 1; i >= 0; i--) |
1238 | | - if ((sid = get_sid_nw(i)) && sid->adapter == aid) { |
1239 | | - mutex_unlock(&sid->mutex); |
1240 | | - ls++; |
1241 | | - } |
1242 | | - return ls; |
1243 | | -} |
1244 | | - |
1245 | 1241 | void free_all_streams() { |
1246 | 1242 | int i; |
1247 | 1243 | std::lock_guard<SMutex> lock(st_mutex); |
|
0 commit comments