-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathops.c
1613 lines (1312 loc) · 35.8 KB
/
ops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "debug.h"
#include "ops.h"
#include "parser.h"
#include "thread-pool.h"
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <poll.h>
#include <stdbool.h>
#include <string.h>
#include <sys/eventfd.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
int yyparse(yyscan_t scanner);
struct DevEntry;
/* Corresponds to a thread reading from a device */
struct ThdEntry {
SLIST_ENTRY(ThdEntry) parser_list_entry;
SLIST_ENTRY(ThdEntry) dev_list_entry;
unsigned int nb, sample_size, samples_count;
ssize_t err;
int eventfd;
struct parser_pdata *pdata;
struct iio_device *dev;
struct DevEntry *entry;
struct iio_channels_mask *mask;
bool active, is_writer, new_client, wait_for_open;
};
static void thd_entry_event_signal(struct ThdEntry *thd)
{
uint64_t e = 1;
int ret;
do {
ret = write(thd->eventfd, &e, sizeof(e));
} while (ret == -1 && errno == EINTR);
}
static int thd_entry_event_wait(struct ThdEntry *thd, pthread_mutex_t *mutex,
int fd_in)
{
struct pollfd pfd[3];
uint64_t e;
int ret;
pthread_mutex_unlock(mutex);
pfd[0].fd = thd->eventfd;
pfd[0].events = POLLIN;
pfd[1].fd = fd_in;
pfd[1].events = POLLRDHUP;
pfd[2].fd = thread_pool_get_poll_fd(thd->pdata->pool);
pfd[2].events = POLLIN;
do {
poll_nointr(pfd, 3);
if ((pfd[1].revents & POLLRDHUP) || (pfd[2].revents & POLLIN)) {
pthread_mutex_lock(mutex);
return -EPIPE;
}
do {
ret = read(thd->eventfd, &e, sizeof(e));
} while (ret == -1 && errno == EINTR);
} while (ret == -1 && errno == EAGAIN);
pthread_mutex_lock(mutex);
return 0;
}
/* Corresponds to an opened device */
struct DevEntry {
unsigned int ref_count;
struct iio_device *dev;
struct iio_buffer *buf;
struct iio_block **blocks;
unsigned int sample_size, nb_clients;
unsigned int samples_count;
bool update_mask;
bool cyclic;
bool closed;
bool cancelled;
unsigned int nb_blocks, curr_block;
/* Linked list of ThdEntry structures corresponding
* to all the threads who opened the device */
SLIST_HEAD(ThdHead, ThdEntry) thdlist_head;
pthread_mutex_t thdlist_lock;
pthread_cond_t rw_ready_cond;
struct iio_channels_mask *mask;
};
struct sample_cb_info {
struct parser_pdata *pdata;
unsigned int nb_bytes, cpt;
};
/* Protects iio_device_{set,get}_data() from concurrent access from multiple
* clients */
static pthread_mutex_t devlist_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int get_channel_number(const struct iio_channel *chn)
{
const struct iio_device *dev = iio_channel_get_device(chn);
const struct iio_channel *other;
unsigned int i = 0;
for (i = 0; i < iio_device_get_channels_count(dev); i++) {
other = iio_device_get_channel(dev, i);
if (other == chn)
break;
}
return i;
}
static inline const char *dev_label_or_name_or_id(const struct iio_device *dev)
{
const char *name;
name = iio_device_get_label(dev);
if (name)
return name;
name = iio_device_get_name(dev);
if (name)
return name;
return iio_device_get_id(dev);
}
static void print_value(struct parser_pdata *pdata, long value)
{
char buf[128];
snprintf(buf, sizeof(buf), "%li\n", value);
output(pdata, buf);
}
static ssize_t send_sample(const struct iio_channel *chn,
void *src, size_t length, void *d)
{
struct sample_cb_info *info = d;
if (info->nb_bytes < length)
return 0;
if (info->cpt % length) {
unsigned int i, goal = length - info->cpt % length;
char zero = 0;
ssize_t ret;
for (i = 0; i < goal; i++) {
ret = info->pdata->writefd(info->pdata, &zero, 1);
if (ret < 0)
return ret;
}
info->cpt += goal;
}
info->cpt += length;
info->nb_bytes -= length;
return write_all(info->pdata, src, length);
}
static ssize_t receive_sample(const struct iio_channel *chn,
void *dst, size_t length, void *d)
{
struct sample_cb_info *info = d;
if (info->cpt == info->nb_bytes)
return 0;
/* Skip the padding if needed */
if (info->cpt % length) {
unsigned int i, goal = length - info->cpt % length;
char foo;
ssize_t ret;
for (i = 0; i < goal; i++) {
ret = info->pdata->readfd(info->pdata, &foo, 1);
if (ret < 0)
return ret;
}
info->cpt += goal;
}
info->cpt += length;
return read_all(info->pdata, dst, length);
}
static ssize_t send_data(struct DevEntry *dev, struct ThdEntry *thd, size_t len)
{
struct parser_pdata *pdata = thd->pdata;
bool demux = server_demux && dev->sample_size != thd->sample_size;
unsigned int i, nb_channels = iio_device_get_channels_count(dev->dev);
unsigned int nb_words = (nb_channels + 31) / 32;
struct iio_block *block = dev->blocks[dev->curr_block];
const struct iio_channels_mask *mask;
const struct iio_channel *chn;
uint32_t *words;
ssize_t ret, length;
void *start;
if (demux)
len = (len / dev->sample_size) * thd->sample_size;
if (len > thd->nb)
len = thd->nb;
print_value(pdata, len);
if (thd->new_client) {
char buf[129], *ptr = buf;
mask = demux ? thd->mask : dev->mask;
words = calloc(nb_words, 4);
if (!words)
return -ENOMEM;
for (i = 0; i < nb_channels; i++) {
chn = iio_device_get_channel(dev->dev, i);
if (iio_channel_is_enabled(chn, mask))
words[BIT_WORD(i)] |= BIT_MASK(i);
}
length = sizeof(buf);
/* Send the current mask */
for (i = nb_words; i > 0 && ptr < buf + sizeof(buf);
i--, ptr += 8) {
snprintf(ptr, length, "%08x", words[i - 1]);
length -= 8;
}
*ptr = '\n';
length--;
free(words);
if (length < 0) {
IIO_ERROR("send_data: string length error\n");
return -ENOSPC;
}
ret = write_all(pdata, buf, ptr + 1 - buf);
if (ret < 0)
return ret;
thd->new_client = false;
}
if (!demux) {
/* Short path */
start = iio_block_start(block);
return write_all(pdata, start, len);
} else {
struct sample_cb_info info = {
.pdata = pdata,
.cpt = 0,
.nb_bytes = len,
};
return iio_block_foreach_sample(block, thd->mask,
send_sample, &info);
}
}
static ssize_t receive_data(struct DevEntry *dev, struct ThdEntry *thd)
{
struct parser_pdata *pdata = thd->pdata;
struct iio_block *block = dev->blocks[dev->curr_block];
void *ptr;
/* Inform that no error occurred, and that we'll start reading data */
if (thd->new_client) {
print_value(thd->pdata, 0);
thd->new_client = false;
}
if (dev->sample_size == thd->sample_size) {
/* Short path: Receive directly in the buffer */
size_t len = dev->sample_size * dev->samples_count;
if (thd->nb < len)
len = thd->nb;
ptr = iio_block_start(block);
return read_all(pdata, ptr, len);
} else {
/* Long path: Mux the samples to the buffer */
struct sample_cb_info info = {
.pdata = pdata,
.cpt = 0,
.nb_bytes = thd->nb,
};
return iio_block_foreach_sample(block, thd->mask,
receive_sample, &info);
}
}
static void dev_entry_put(struct DevEntry *entry)
{
bool free_entry = false;
pthread_mutex_lock(&entry->thdlist_lock);
entry->ref_count--;
if (entry->ref_count == 0)
free_entry = true;
pthread_mutex_unlock(&entry->thdlist_lock);
if (free_entry) {
pthread_mutex_destroy(&entry->thdlist_lock);
pthread_cond_destroy(&entry->rw_ready_cond);
free(entry->mask);
free(entry);
}
}
static void signal_thread(struct ThdEntry *thd, ssize_t ret)
{
thd->err = ret;
thd->nb = 0;
thd->active = false;
thd_entry_event_signal(thd);
}
static int create_buf_and_blocks(struct DevEntry *entry, size_t samples_count,
struct iio_channels_mask *mask)
{
struct iio_device_pdata *dev_pdata;
size_t buf_size;
unsigned int nb_blocks;
unsigned int i;
int err;
dev_pdata = iio_device_get_data(entry->dev);
nb_blocks = dev_pdata->nb_blocks;
entry->blocks = calloc(nb_blocks, sizeof(*entry->blocks));
if (!entry->blocks)
return -ENOMEM;
entry->buf = iio_device_create_buffer(entry->dev, 0, mask);
err = iio_err(entry->buf);
if (err)
goto err_free_blocks_array;
buf_size = samples_count * iio_device_get_sample_size(entry->dev, mask);
for (i = 0; i < nb_blocks; i++) {
entry->blocks[i] = iio_buffer_create_block(entry->buf, buf_size);
err = iio_err(entry->blocks[i]);
if (err)
goto err_free_blocks;
}
entry->nb_blocks = nb_blocks;
entry->curr_block = 0;
return 0;
err_free_blocks:
for (; i; i--)
iio_block_destroy(entry->blocks[i - 1]);
iio_buffer_destroy(entry->buf);
err_free_blocks_array:
entry->buf = NULL;
free(entry->blocks);
entry->blocks = NULL;
return err;
}
static void free_buf_and_blocks(struct DevEntry *entry)
{
unsigned int i;
if (entry->buf) {
IIO_DEBUG("Disable buffer...\n");
iio_buffer_disable(entry->buf);
IIO_DEBUG("Disabled\n");
}
for (i = 0; i < entry->nb_blocks; i++)
if (entry->blocks[i])
iio_block_destroy(entry->blocks[i]);
if (entry->buf) {
iio_buffer_destroy(entry->buf);
IIO_DEBUG("Buffer destroyed.\n");
entry->buf = NULL;
}
free(entry->blocks);
entry->nb_blocks = 0;
}
static void rw_thd(struct thread_pool *pool, void *d)
{
struct DevEntry *entry = d;
struct ThdEntry *thd, *next_thd;
struct iio_device *dev = entry->dev;
struct iio_device_pdata *dev_pdata;
unsigned int i, nb_channels = iio_device_get_channels_count(dev);
struct iio_channel *chn;
struct iio_block *block;
ssize_t nb_bytes, ret = 0;
IIO_DEBUG("R/W thread started for device %s\n",
dev_label_or_name_or_id(dev));
while (true) {
bool has_readers = false, has_writers = false,
mask_updated = false;
unsigned int sample_size;
/* NOTE: this while loop must exit with thdlist_lock locked. */
pthread_mutex_lock(&entry->thdlist_lock);
if (SLIST_EMPTY(&entry->thdlist_head))
break;
if (entry->update_mask) {
unsigned int i;
unsigned int samples_count = 0;
free_buf_and_blocks(entry);
for (i = 0; i < nb_channels; i++) {
chn = iio_device_get_channel(dev, i);
iio_channel_disable(chn, entry->mask);
}
SLIST_FOREACH(thd, &entry->thdlist_head, dev_list_entry) {
for (i = 0; i < nb_channels; i++) {
chn = iio_device_get_channel(dev, i);
if (iio_channel_is_enabled(chn, thd->mask))
iio_channel_enable(chn, entry->mask);
}
if (thd->samples_count > samples_count)
samples_count = thd->samples_count;
}
ret = create_buf_and_blocks(entry, samples_count, entry->mask);
if (ret) {
IIO_ERROR("Unable to create buffer\n");
break;
}
entry->cancelled = false;
/* Enqueue empty blocks, to make sure they can be queued with data */
for (i = 0; !ret && i < entry->nb_blocks; i++)
ret = iio_block_enqueue(entry->blocks[i], 0, false);
if (i < entry->nb_blocks) {
IIO_ERROR("Unable to enqueue blocks\n");
break;
}
ret = iio_buffer_enable(entry->buf);
if (ret) {
IIO_ERROR("Unable to enable buffer\n");
break;
}
/* Signal the threads that we opened the device */
SLIST_FOREACH(thd, &entry->thdlist_head, dev_list_entry) {
if (thd->wait_for_open) {
thd->wait_for_open = false;
signal_thread(thd, 0);
}
}
IIO_DEBUG("IIO device %s reopened with new mask\n",
dev_label_or_name_or_id(dev));
entry->update_mask = false;
entry->sample_size = iio_device_get_sample_size(dev, entry->mask);
entry->samples_count = samples_count;
mask_updated = true;
}
sample_size = entry->sample_size;
SLIST_FOREACH(thd, &entry->thdlist_head, dev_list_entry) {
thd->active = !thd->err && thd->nb >= sample_size;
if (mask_updated && thd->active)
signal_thread(thd, thd->nb);
if (thd->is_writer)
has_writers |= thd->active;
else
has_readers |= thd->active;
}
if (!has_readers && !has_writers) {
pthread_cond_wait(&entry->rw_ready_cond,
&entry->thdlist_lock);
}
pthread_mutex_unlock(&entry->thdlist_lock);
if (!has_readers && !has_writers)
continue;
block = entry->blocks[entry->curr_block];
ret = iio_block_dequeue(block, false);
pthread_mutex_lock(&entry->thdlist_lock);
if (ret < 0) {
/* Reading from the device failed - signal the
* error to all connected clients. */
/* Don't use SLIST_FOREACH - see comment below */
for (thd = SLIST_FIRST(&entry->thdlist_head);
thd; thd = next_thd) {
next_thd = SLIST_NEXT(thd, dev_list_entry);
if (!thd->active || thd->is_writer)
continue;
signal_thread(thd, ret);
}
pthread_mutex_unlock(&entry->thdlist_lock);
continue;
}
/*
* When the last client disconnects the buffer is
* cancelled and iio_buffer_refill() returns an error. A
* new client might have connected before we got here
* though, in that case the rw thread has to stay active
* and a new buffer is created. If the list is still empty the loop
* will exit normally.
*/
if (entry->cancelled) {
pthread_mutex_unlock(&entry->thdlist_lock);
continue;
}
if (has_readers) {
nb_bytes = iio_block_end(block) - iio_block_start(block);
/* We don't use SLIST_FOREACH here. As soon as a thread is
* signaled, its "thd" structure might be freed;
* SLIST_FOREACH would then cause a segmentation fault, as it
* reads "thd" to get the address of the next element. */
for (thd = SLIST_FIRST(&entry->thdlist_head);
thd; thd = next_thd) {
next_thd = SLIST_NEXT(thd, dev_list_entry);
if (!thd->active || thd->is_writer)
continue;
ret = send_data(entry, thd, nb_bytes);
if (ret > 0)
thd->nb -= ret;
if (ret < 0 || thd->nb < sample_size)
signal_thread(thd, (ret < 0) ?
ret : (ssize_t) thd->nb);
}
}
nb_bytes = 0;
if (has_writers) {
/* Reset the size of the buffer to its maximum size.
*
* XXX(pcercuei): There is no way to perform this with
* the public libiio API. However, it probably does not
* matter; we only need to reset the size of the buffer
* if the buffer was used for receiving samples, and
* to date there is no IIO device that supports both
* receiving and sending samples.
*
* entry->buf->data_length = entry->buf->length;
*/
/* Same comment as above */
for (thd = SLIST_FIRST(&entry->thdlist_head);
thd; thd = next_thd) {
next_thd = SLIST_NEXT(thd, dev_list_entry);
if (!thd->active || !thd->is_writer)
continue;
ret = receive_data(entry, thd);
if (ret > 0) {
thd->nb -= ret;
if (ret > nb_bytes)
nb_bytes = ret;
}
if (ret < 0)
signal_thread(thd, ret);
}
}
ret = iio_block_enqueue(block, nb_bytes, entry->cyclic);
entry->curr_block = (entry->curr_block + 1) % entry->nb_blocks;
if (entry->cancelled) {
pthread_mutex_unlock(&entry->thdlist_lock);
continue;
}
if (has_writers) {
/* Signal threads which completed their RW command */
for (thd = SLIST_FIRST(&entry->thdlist_head);
thd; thd = next_thd) {
next_thd = SLIST_NEXT(thd, dev_list_entry);
if (!thd->active || !thd->is_writer)
continue;
if (ret < 0)
signal_thread(thd, ret);
else if (thd->nb < sample_size)
signal_thread(thd, thd->nb);
}
}
pthread_mutex_unlock(&entry->thdlist_lock);
}
/* Signal all remaining threads */
for (thd = SLIST_FIRST(&entry->thdlist_head); thd; thd = next_thd) {
next_thd = SLIST_NEXT(thd, dev_list_entry);
SLIST_REMOVE(&entry->thdlist_head, thd, ThdEntry, dev_list_entry);
thd->wait_for_open = false;
signal_thread(thd, ret);
}
free_buf_and_blocks(entry);
entry->closed = true;
pthread_mutex_unlock(&entry->thdlist_lock);
pthread_mutex_lock(&devlist_lock);
/* It is possible that a new thread has already started, make sure to
* not overwrite it. */
dev_pdata = iio_device_get_data(dev);
if (dev_pdata->entry == entry)
dev_pdata->entry = NULL;
pthread_mutex_unlock(&devlist_lock);
IIO_DEBUG("Stopping R/W thread for device %s\n",
dev_label_or_name_or_id(dev));
dev_entry_put(entry);
}
static struct ThdEntry *parser_lookup_thd_entry(struct parser_pdata *pdata,
struct iio_device *dev)
{
struct ThdEntry *t;
SLIST_FOREACH(t, &pdata->thdlist_head, parser_list_entry) {
if (t->dev == dev)
return t;
}
return NULL;
}
static ssize_t rw_buffer(struct parser_pdata *pdata,
struct iio_device *dev, unsigned int nb, bool is_write)
{
struct DevEntry *entry;
struct ThdEntry *thd;
ssize_t ret;
if (!dev)
return -ENODEV;
thd = parser_lookup_thd_entry(pdata, dev);
if (!thd)
return -EBADF;
entry = thd->entry;
if (nb < entry->sample_size)
return 0;
pthread_mutex_lock(&entry->thdlist_lock);
if (entry->closed) {
pthread_mutex_unlock(&entry->thdlist_lock);
return -EBADF;
}
if (thd->nb) {
pthread_mutex_unlock(&entry->thdlist_lock);
return -EBUSY;
}
thd->new_client = true;
thd->nb = nb;
thd->err = 0;
thd->is_writer = is_write;
thd->active = true;
pthread_cond_signal(&entry->rw_ready_cond);
IIO_DEBUG("Waiting for completion...\n");
while (thd->active) {
ret = thd_entry_event_wait(thd, &entry->thdlist_lock, pdata->fd_in);
if (ret)
break;
}
if (ret == 0)
ret = thd->err;
pthread_mutex_unlock(&entry->thdlist_lock);
if (ret > 0 && ret < (ssize_t) nb)
print_value(thd->pdata, 0);
IIO_DEBUG("Exiting rw_buffer with code %li\n", (long) ret);
if (ret < 0)
return ret;
else
return nb - ret;
}
static void get_mask(const char *mask, size_t len, uint32_t *words)
{
size_t nb = (len + 7) / 8;
uint32_t *ptr = words + nb;
char buf[9];
while (*mask) {
snprintf(buf, sizeof(buf), "%.*s", 8, mask);
sscanf(buf, "%08x", --ptr);
mask += 8;
IIO_DEBUG("Mask[%lu]: 0x%08x\n",
(unsigned long) (words - ptr) / 4, *ptr);
}
}
static void free_thd_entry(struct ThdEntry *t)
{
close(t->eventfd);
free(t->mask);
free(t);
}
static void remove_thd_entry(struct ThdEntry *t)
{
struct DevEntry *entry = t->entry;
pthread_mutex_lock(&entry->thdlist_lock);
if (!entry->closed) {
entry->update_mask = true;
SLIST_REMOVE(&entry->thdlist_head, t, ThdEntry, dev_list_entry);
if (SLIST_EMPTY(&entry->thdlist_head) && entry->buf) {
entry->cancelled = true;
iio_buffer_cancel(entry->buf); /* Wakeup the rw thread */
}
pthread_cond_signal(&entry->rw_ready_cond);
}
pthread_mutex_unlock(&entry->thdlist_lock);
dev_entry_put(entry);
free_thd_entry(t);
}
static ssize_t get_dev_sample_size_mask(const struct iio_device *dev,
const uint32_t *mask, size_t words)
{
unsigned int i, len, number,
nb_channels = iio_device_get_channels_count(dev);
const struct iio_channel *prev = NULL;
const struct iio_channel *chn;
const struct iio_data_format *fmt;
long index;
ssize_t size = 0;
if (words != (nb_channels + 31) / 32)
return -EINVAL;
for (i = 0; i < nb_channels; i++) {
chn = iio_device_get_channel(dev, i);
number = get_channel_number(chn);
fmt = iio_channel_get_data_format(chn);
index = iio_channel_get_index(chn);
len = fmt->length / 8 * fmt->repeat;
if (index < 0)
break;
if (!TEST_BIT(mask, number))
continue;
if (prev && index == iio_channel_get_index(prev)) {
prev = chn;
continue;
}
if (size % len)
size += 2 * len - (size % len);
else
size += len;
prev = chn;
}
return size;
}
static int open_dev_helper(struct parser_pdata *pdata, struct iio_device *dev,
size_t samples_count, uint32_t *words, bool cyclic)
{
int ret = -ENOMEM;
struct DevEntry *entry;
struct ThdEntry *thd;
unsigned int cyclic_retry = 500;
unsigned int i, nb_channels = iio_device_get_channels_count(dev);
struct iio_channels_mask *mask;
const struct iio_channel *chn;
struct iio_device_pdata *dev_pdata;
mask = iio_create_channels_mask(nb_channels);
if (!mask)
return -ENOMEM;
for (i = 0; i < nb_channels; i++) {
chn = iio_device_get_channel(dev, i);
if (TEST_BIT(words, i))
iio_channel_enable(chn, mask);
}
thd = zalloc(sizeof(*thd));
if (!thd)
goto err_free_mask;
thd->mask = mask;
thd->wait_for_open = true;
thd->nb = 0;
thd->samples_count = samples_count;
thd->sample_size = iio_device_get_sample_size(dev, mask);
thd->pdata = pdata;
thd->dev = dev;
thd->eventfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
dev_pdata = iio_device_get_data(dev);
retry:
/* Atomically look up the thread and make sure that it is still active
* or allocate new one. */
pthread_mutex_lock(&devlist_lock);
entry = dev_pdata->entry;
if (entry) {
if (cyclic || entry->cyclic) {
/* Only one client allowed in cyclic mode */
pthread_mutex_unlock(&devlist_lock);
/* There is an inherent race condition if a client
* creates a new cyclic buffer shortly after destroying
* a previous. E.g. like
*
* iio_buffer_destroy(buf);
* buf = iio_device_create_buffer(dev, n, true);
*
* In this case the two buffers each use their own
* communication channel which are unordered to each
* other. E.g. the socket open might arrive before the
* socket close on the host side, even though they were
* sent in the opposite order on the client side. This
* race condition can cause an error being reported back
* to the client, even though the code on the client
* side was well formed and would work fine e.g. using
* the local backend.
*
* To avoid this issue go to sleep for up to 50ms in
* intervals of 100us. This should be enough time for
* the issue to resolve itself. If there actually is
* contention on the buffer an error will eventually be
* returned in which case the additional delay cause by
* the retires should not matter too much.
*
* This is not pretty but it works.
*/
if (cyclic_retry) {
struct timespec wait;
wait.tv_sec = 0;
wait.tv_nsec = (100 * 1000);
cyclic_retry--;
nanosleep(&wait, &wait);
goto retry;
}
ret = -EBUSY;
goto err_free_thd;
}
pthread_mutex_lock(&entry->thdlist_lock);
if (!entry->closed) {
pthread_mutex_unlock(&devlist_lock);
entry->ref_count++;
SLIST_INSERT_HEAD(&entry->thdlist_head, thd, dev_list_entry);
thd->entry = entry;
entry->update_mask = true;
IIO_DEBUG("Added thread to client list\n");
pthread_cond_signal(&entry->rw_ready_cond);
/* Wait until the device is opened by the rw thread */
while (thd->wait_for_open) {
ret = thd_entry_event_wait(thd, &entry->thdlist_lock, pdata->fd_in);
if (ret)
break;
}
pthread_mutex_unlock(&entry->thdlist_lock);
if (ret == 0)
ret = (int) thd->err;
if (ret < 0)
remove_thd_entry(thd);
else
SLIST_INSERT_HEAD(&pdata->thdlist_head, thd, parser_list_entry);
return ret;
} else {
pthread_mutex_unlock(&entry->thdlist_lock);
}
}
entry = zalloc(sizeof(*entry));
if (!entry) {
pthread_mutex_unlock(&devlist_lock);
goto err_free_thd;
}
entry->ref_count = 2; /* One for thread, one for the client */
entry->mask = iio_create_channels_mask(nb_channels);
if (!entry->mask) {
pthread_mutex_unlock(&devlist_lock);
goto err_free_entry;
}
entry->cyclic = cyclic;
entry->update_mask = true;
entry->dev = dev;
entry->buf = NULL;
SLIST_INIT(&entry->thdlist_head);
SLIST_INSERT_HEAD(&entry->thdlist_head, thd, dev_list_entry);
thd->entry = entry;
IIO_DEBUG("Added thread to client list\n");
pthread_mutex_init(&entry->thdlist_lock, NULL);
pthread_cond_init(&entry->rw_ready_cond, NULL);
ret = thread_pool_add_thread(main_thread_pool, rw_thd, entry, "rw_thd");
if (ret) {
pthread_mutex_unlock(&devlist_lock);
goto err_free_entry_mask;
}
IIO_DEBUG("Adding new device thread to device list\n");
dev_pdata->entry = entry;
pthread_mutex_unlock(&devlist_lock);
pthread_mutex_lock(&entry->thdlist_lock);
/* Wait until the device is opened by the rw thread */
while (thd->wait_for_open) {
ret = thd_entry_event_wait(thd, &entry->thdlist_lock, pdata->fd_in);
if (ret)
break;