summaryrefslogtreecommitdiff
path: root/map65/libm65/npar_ptrs_mod.f90
blob: 99bde665a053dabd4ba3973a8e1e48e560b5794f (plain)
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
module npar_ptrs_mod
  use iso_c_binding
  implicit none

  type :: decode_parameters
    real(c_double) :: fcenter = 0
    real(c_double) :: ftol_nb = 0
    integer(c_int) :: nfa = 0
    integer(c_int) :: nfb = 0
    integer(c_int) :: nfshift = 0
    integer(c_int) :: nfcal = 0
    integer(c_int) :: nutc = 0
    integer(c_int) :: idphi = 0
    integer(c_int) :: mousedf = 0
    integer(c_int) :: mousefqso = 0
    integer(c_int) :: nagain = 0
    integer(c_int) :: ndepth = 0
    integer(c_int) :: ndiskdat = 0
    integer(c_int) :: neme = 0
    integer(c_int) :: newdat = 0
    integer(c_int) :: map65RxLog = 0
    integer(c_int) :: mcall3 = 0
    integer(c_int) :: ntimeout = 0
    integer(c_int) :: ntol = 0
    integer(c_int) :: nxant = 0
    integer(c_int) :: nfsample = 0
    integer(c_int) :: nxpol = 0
    integer(c_int) :: nmode = 0
    integer(c_int) :: nsave = 0
    integer(c_int) :: max_drift = 0
    integer(c_int) :: nhsym = 0
    integer(c_int) :: ndop00 = 0
    integer(c_int) :: nrxlog = 0
    integer(c_int) :: nkeep = 0
    integer(c_int) :: manualDecodeFlag = 0
    character(len=12) :: mycall = ''
    character(len=12) :: hiscall = ''
    character(len=6) :: mygrid = ''
    character(len=6) :: hisgrid = ''
    character(len=20) :: datetime = ''
  end type

  ! ---------------------------------------------------------------------------
  ! Decode parameters: draft vs. active
  !
  ! Two copies of decode_parameters exist, and which one you touch matters.
  !
  !   draft   Owned by the GUI thread. Every set_xxx()/get_xxx() bind(C)
  !           routine in this module reads or writes draft, under
  !           lock_decode_requests(). It holds the *pending* GUI state, not
  !           necessarily what the decoder is using right now.
  !
  !   active  Owned by the decode thread. The module pointers below (nkeep,
  !           ntimeout, mycall, ...) alias fields of active, so Fortran decode
  !           code that says "ntimeout" is reading active%ntimeout.
  !
  ! Flow:
  !   MainWindow::decode() -> set_xxx() writes draft
  !   publish_decode_request() copies draft into a queued request (forcing
  !                            newdat = 1 in that copy) and clears
  !                            draft%manualDecodeFlag
  !   claim_decode_request()   copies the oldest request into active; an
  !                            automatic request is marked expired, and run_m65
  !                            skips it (<DecodeSkipped>), when the data
  !                            snapshot it was published with has been
  !                            overwritten or never filled (or, for requests
  !                            without a snapshot, when its input generation
  !                            is out of date). A request whose snapshot is
  !                            intact still runs after the minute rolls over.
  !   initialize_decode_parameters() copies draft into active once, at startup
  !   The decoder then sees a consistent snapshot for the whole pass, even if
  !   the GUI changes draft meanwhile.
  !
  ! Why a lock alone is not enough: a decode pass runs for many seconds and
  ! reads its parameters throughout. The lock only protects the instant of a
  ! write, so if setters wrote the decoder's own variables directly, a GUI
  ! change mid-pass would still alter values partway through the decode. The
  ! draft/active copy is what freezes the parameters for a pass and lets
  ! requests queue; the lock keeps publish_decode_request from copying a
  ! half-written draft.
  !
  ! Rules:
  !   * C++ callers: a get_xxx() returns draft, i.e. the pending GUI value,
  !     not the value the running decode is using.
  !   * A parameter only reaches the decoder if the GUI writes it to draft
  !     before a request is published. Setting it once and never re-pushing
  !     works only if nothing else overwrites it (decode() currently re-pushes
  !     most values every cycle).
  !   * Fortran code may write active fields only for state it owns (e.g.
  !     manualDecodeFlag = 0, nkeep = ...). Such writes do NOT flow back to
  !     draft, so the next published request will carry draft's old value.
  !     If a value must persist across requests, change it in draft too.
  !   * Never add a second variable for the same setting (cf. the old
  !     map65RxLog vs. nrxlog bug, where the setter wrote one variable and the
  !     decoder read the other).
  ! ---------------------------------------------------------------------------
  type(decode_parameters), target, save :: active
  type(decode_parameters), save :: draft
  type :: decode_request
    type(decode_parameters) :: parameters
    integer(c_int64_t) :: id
    integer(c_int64_t) :: input_generation
    integer(c_int) :: snap_idx = 0            ! snapshot slot (1 = nhsym1, 2 = nhsym2); 0 = none
    integer(c_int64_t) :: snap_seq = 0        ! fill number of that slot when published
    type(decode_request), pointer :: next => null()
  end type
  type(decode_request), pointer, save :: pending_head => null(), pending_tail => null()
  integer(c_int64_t), save :: published_request_id = 0
  integer(c_int64_t), save :: active_request_id = 0
  integer(c_int64_t), save :: live_input_generation = 0
  integer(c_int64_t), save :: active_input_generation = 0
  logical, save :: active_request_expired = .false.

  ! Decoder data snapshots (see datcom_ptrs_mod). All of this state is
  ! protected by lock_decode_requests().
  !   snap_seq(i)       fill number, bumped each time slot i is (re)filled
  !   snap_valid_seq(i) fill number whose contents are intact; 0 while a fill
  !                     is in progress or after a fill was skipped
  !   snap_busy(i)      slot i is being read by the running decode pass; a fill
  !                     is skipped rather than overwrite it
  integer(c_int64_t), save :: snap_seq(2) = 0
  integer(c_int64_t), save :: snap_valid_seq(2) = 0
  logical, save :: snap_busy(2) = .false.
  integer(c_int), save :: snap_latest_idx = 0   ! most recently completed fill
  integer(c_int), save :: active_snap_idx = 0   ! slot read by the active pass

  real(c_double), pointer :: fcenter => active%fcenter
  real(c_double), pointer :: ftol_nb => active%ftol_nb
  integer(c_int), pointer :: nfa => active%nfa
  integer(c_int), pointer :: nfb => active%nfb
  integer(c_int), pointer :: nfshift => active%nfshift
  integer(c_int), pointer :: nfcal => active%nfcal
  integer(c_int), pointer :: nutc => active%nutc
  integer(c_int), pointer :: idphi => active%idphi
  integer(c_int), pointer :: mousedf => active%mousedf
  integer(c_int), pointer :: mousefqso => active%mousefqso
  integer(c_int), pointer :: nagain => active%nagain
  integer(c_int), pointer :: ndepth => active%ndepth
  integer(c_int), pointer :: ndiskdat => active%ndiskdat
  integer(c_int), pointer :: neme => active%neme
  integer(c_int), pointer :: newdat => active%newdat
  integer(c_int), pointer :: map65RxLog => active%map65RxLog
  integer(c_int), pointer :: mcall3 => active%mcall3
  integer(c_int), pointer :: ntimeout => active%ntimeout
  integer(c_int), pointer :: ntol => active%ntol
  integer(c_int), pointer :: nxant => active%nxant
  integer(c_int), pointer :: nfsample => active%nfsample
  integer(c_int), pointer :: nxpol => active%nxpol
  integer(c_int), pointer :: nmode => active%nmode
  integer(c_int), pointer :: nsave => active%nsave
  integer(c_int), pointer :: max_drift => active%max_drift
  integer(c_int), pointer :: nhsym => active%nhsym
  integer(c_int), pointer :: ndop00 => active%ndop00
  integer(c_int), pointer :: nrxlog => active%nrxlog
  integer(c_int), pointer :: nkeep => active%nkeep
  integer(c_int), pointer :: manualDecodeFlag => active%manualDecodeFlag
  character(len=12), pointer :: mycall => active%mycall
  character(len=12), pointer :: hiscall => active%hiscall
  character(len=6), pointer :: mygrid => active%mygrid
  character(len=6), pointer :: hisgrid => active%hisgrid
  character(len=20), pointer :: datetime => active%datetime
  integer(c_int) :: stop_m65 = 0
  integer(c_int) :: decoder_ready = 0
  character(len=512) :: wsjtx_dir = ''

  integer(c_int) :: nrate_active    = 96000
  integer(c_int) :: nfft_active     = 32768
  integer(c_int) :: nsmax_active    = 60*96000
  integer(c_int) :: nfft_big_active = 56*96000
  integer(c_int) :: t_start = -1
  logical(c_bool) :: abort_decode = .false.

  interface
    subroutine lock_decode_requests() bind(C)
    end subroutine
    subroutine unlock_decode_requests() bind(C)
    end subroutine
  end interface

contains

  subroutine set_manual_decode_flag(val) bind(C, name="set_manual_decode_flag")
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%manualDecodeFlag = val
    call unlock_decode_requests()
  end subroutine

  function get_manual_decode_flag() bind(C, name="get_manual_decode_flag")
      use iso_c_binding
      integer(c_int) :: get_manual_decode_flag
    call lock_decode_requests()
      get_manual_decode_flag = draft%manualDecodeFlag
    call unlock_decode_requests()
  end function
  
  subroutine set_ftol_nb(val) bind(C, name="set_ftol_nb")
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ftol_nb = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_runtime_params(rate_hz, nfft, nfft_big) bind(C, name="set_runtime_params_")
    use iso_c_binding
    implicit none
    integer(c_int), value :: rate_hz, nfft, nfft_big

    nrate_active    = rate_hz
    nfft_active     = nfft
    nsmax_active    = 60*rate_hz
    nfft_big_active = nfft_big
  end subroutine set_runtime_params

  subroutine set_wsjtx_dir(path, path_len) bind(C, name="set_wsjtx_dir_")
    use iso_c_binding
    implicit none
    character(kind=c_char), dimension(*), intent(in) :: path
    integer(c_int), value :: path_len
    integer :: n

    wsjtx_dir = ''
    if (path_len > 0) then
       n = min(path_len, len(wsjtx_dir))
       wsjtx_dir(1:n) = transfer(path(1:n), wsjtx_dir(1:n))
    end if
  end subroutine set_wsjtx_dir


  subroutine set_stop_m65(val) bind(C, name="set_stop_m65")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    stop_m65 = 0
    if (val /= 0) then
      stop_m65 = 1
      call clear_pending_requests()
    endif
    call unlock_decode_requests()
  end subroutine

  subroutine set_decoder_ready(val) bind(C, name="set_decoder_ready")
    use iso_c_binding
    integer(c_int), value :: val    
    call lock_decode_requests()
    decoder_ready = 0
    if (val /= 0) decoder_ready = 1
    call unlock_decode_requests()
  end subroutine
    
  subroutine get_mycall(buf) bind(C, name="get_mycall")
    character(kind=c_char), intent(out) :: buf(12)
    integer :: i

    call lock_decode_requests()
    do i = 1, 12
      buf(i) = draft%mycall(i:i)
    end do
    call unlock_decode_requests()
  end subroutine get_mycall

  subroutine get_hiscall(buf) bind(C, name="get_hiscall")
    character(kind=c_char), intent(out) :: buf(12)
    integer :: i
    call lock_decode_requests()
    do i = 1, 12
      buf(i) = draft%hiscall(i:i)
    end do
    call unlock_decode_requests()
  end subroutine

  subroutine get_mygrid(buf) bind(C, name="get_mygrid")
    character(kind=c_char), intent(out) :: buf(6)
    integer :: i
    call lock_decode_requests()
    do i = 1, 6
      buf(i) = draft%mygrid(i:i)
    end do
    call unlock_decode_requests()
  end subroutine

  subroutine get_hisgrid(buf) bind(C, name="get_hisgrid")
    character(kind=c_char), intent(out) :: buf(6)
    integer :: i
    call lock_decode_requests()
    do i = 1, 6
      buf(i) = draft%hisgrid(i:i)
    end do
    call unlock_decode_requests()
  end subroutine

! NOTE ABOUT DATETIME LENGTH (17 vs 20)
!
! datetime is declared as CHARACTER(len=20) for legacy compatibility with the
! old COMMON block, but only the first 17 characters are meaningful. The decoder
! in m65_mod writes:
!
!     write(..., '( "UTC Date: ", a17 )') datetime(1:17)
!
! so the timestamp format is always 17 characters (YYYY-MM-DD HH:MM). The extra
! 3 characters are unused padding. The C interface therefore exchanges only the
! first 17 bytes, which is intentional and correct.
!
! If the timestamp format changes in the future, this interface boundary should
! be updated accordingly.

  subroutine get_datetime(buf) bind(C, name="get_datetime")
    character(kind=c_char), intent(out) :: buf(17)
    integer :: i
    call lock_decode_requests()
    do i = 1, 17
      buf(i) = draft%datetime(i:i)
    end do
    call unlock_decode_requests()
  end subroutine

  ! Helper subroutine to copy fixed-length Fortran string to C buffer
  subroutine move_chars(dest, dest_len, src)
    integer(c_int), intent(in) :: dest_len
    character(kind=c_char), intent(out) :: dest(dest_len)
    character(len=*), intent(in) :: src
    integer :: i, n

    n = min(len(src), dest_len)
    do i = 1, n
      dest(i) = src(i:i)
    end do
  end subroutine move_chars
   
  function get_fcenter() bind(C, name="get_fcenter")
    real(c_double) :: get_fcenter
    call lock_decode_requests()
    get_fcenter = draft%fcenter
    call unlock_decode_requests()
  end function
  
  function get_nkeep() bind(C, name="get_nkeep")
    integer(c_int) :: get_nkeep
    call lock_decode_requests()
    get_nkeep = draft%nkeep
    call unlock_decode_requests()
  end function
  
  function get_ndop00() bind(C, name="get_ndop00")
    integer(c_int) :: get_ndop00
    call lock_decode_requests()
    get_ndop00 = draft%ndop00
    call unlock_decode_requests()
  end function
  
  function get_map65RxLog() bind(C, name="get_map65RxLog")
    integer(c_int) :: get_map65RxLog
    call lock_decode_requests()
    get_map65RxLog = draft%map65RxLog
    call unlock_decode_requests()
  end function
  
  function get_nutc() bind(C, name="get_nutc")
    integer(c_int) :: get_nutc
    call lock_decode_requests()
    get_nutc = draft%nutc
    call unlock_decode_requests()
  end function

  function get_idphi() bind(C, name="get_idphi")
    integer(c_int) :: get_idphi
    call lock_decode_requests()
    get_idphi = draft%idphi
    call unlock_decode_requests()
  end function

  function get_mousedf() bind(C, name="get_mousedf")
    integer(c_int) :: get_mousedf
    call lock_decode_requests()
    get_mousedf = draft%mousedf
    call unlock_decode_requests()
  end function

  function get_mousefqso() bind(C, name="get_mousefqso")
    integer(c_int) :: get_mousefqso
    call lock_decode_requests()
    get_mousefqso = draft%mousefqso
    call unlock_decode_requests()
  end function

  function get_nagain() bind(C, name="get_nagain")
    integer(c_int) :: get_nagain
    call lock_decode_requests()
    get_nagain = draft%nagain
    call unlock_decode_requests()
  end function

  function get_ndepth() bind(C, name="get_ndepth")
    integer(c_int) :: get_ndepth
    call lock_decode_requests()
    get_ndepth = draft%ndepth
    call unlock_decode_requests()
  end function

  function get_ndiskdat() bind(C, name="get_ndiskdat")
    integer(c_int) :: get_ndiskdat
    call lock_decode_requests()
    get_ndiskdat = draft%ndiskdat
    call unlock_decode_requests()
  end function

  function get_neme() bind(C, name="get_neme")
    integer(c_int) :: get_neme
    call lock_decode_requests()
    get_neme = draft%neme
    call unlock_decode_requests()
  end function

  function get_newdat() bind(C, name="get_newdat")
    integer(c_int) :: get_newdat
    call lock_decode_requests()
    get_newdat = draft%newdat
    call unlock_decode_requests()
  end function

  function get_nfa() bind(C, name="get_nfa")
    integer(c_int) :: get_nfa
    call lock_decode_requests()
    get_nfa = draft%nfa
    call unlock_decode_requests()
  end function

  function get_nfb() bind(C, name="get_nfb")
    integer(c_int) :: get_nfb
    call lock_decode_requests()
    get_nfb = draft%nfb
    call unlock_decode_requests()
  end function

  function get_nfcal() bind(C, name="get_nfcal")
    integer(c_int) :: get_nfcal
    call lock_decode_requests()
    get_nfcal = draft%nfcal
    call unlock_decode_requests()
  end function

  function get_nfshift() bind(C, name="get_nfshift")
    integer(c_int) :: get_nfshift
    call lock_decode_requests()
    get_nfshift = draft%nfshift
    call unlock_decode_requests()
  end function

  function get_mcall3() bind(C, name="get_mcall3")
    integer(c_int) :: get_mcall3
    call lock_decode_requests()
    get_mcall3 = draft%mcall3
    call unlock_decode_requests()
  end function

  function get_ntimeout() bind(C, name="get_ntimeout")
    integer(c_int) :: get_ntimeout
    call lock_decode_requests()
    get_ntimeout = draft%ntimeout
    call unlock_decode_requests()
  end function

  function get_ntol() bind(C, name="get_ntol")
    integer(c_int) :: get_ntol
    call lock_decode_requests()
    get_ntol = draft%ntol
    call unlock_decode_requests()
  end function

  function get_nxant() bind(C, name="get_nxant")
    integer(c_int) :: get_nxant
    call lock_decode_requests()
    get_nxant = draft%nxant
    call unlock_decode_requests()
  end function

  function get_nfsample() bind(C, name="get_nfsample")
    integer(c_int) :: get_nfsample
    call lock_decode_requests()
    get_nfsample = draft%nfsample
    call unlock_decode_requests()
  end function

  function get_nxpol() bind(C, name="get_nxpol")
    integer(c_int) :: get_nxpol
    call lock_decode_requests()
    get_nxpol = draft%nxpol
    call unlock_decode_requests()
  end function

  function get_nmode() bind(C, name="get_nmode")
    integer(c_int) :: get_nmode
    call lock_decode_requests()
    get_nmode = draft%nmode
    call unlock_decode_requests()
  end function

  function get_nsave() bind(C, name="get_nsave")
    integer(c_int) :: get_nsave
    call lock_decode_requests()
    get_nsave = draft%nsave
    call unlock_decode_requests()
  end function

  function get_max_drift() bind(C, name="get_max_drift")
    integer(c_int) :: get_max_drift
    call lock_decode_requests()
    get_max_drift = draft%max_drift
    call unlock_decode_requests()
  end function

  function get_nhsym() bind(C, name="get_nhsym")
    integer(c_int) :: get_nhsym
    call lock_decode_requests()
    get_nhsym = draft%nhsym
    call unlock_decode_requests()
  end function  

  subroutine set_fcenter(val) bind(C, name="set_fcenter")
  use iso_c_binding
  real(c_double), value :: val
    call lock_decode_requests()
  draft%fcenter = val
    call unlock_decode_requests()
  end subroutine
  
  subroutine set_nkeep(val) bind(C, name="set_nkeep")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nkeep = val
    call unlock_decode_requests()
  end subroutine
  
  subroutine set_ndop00(val) bind(C, name="set_ndop00")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ndop00 = val
    call unlock_decode_requests()
  end subroutine
  
  subroutine set_map65RxLog(val) bind(C, name="set_map65RxLog")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%map65RxLog = val
    draft%nrxlog = val
    call unlock_decode_requests()
  end subroutine
  
  subroutine set_nutc(val) bind(C, name="set_nutc")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nutc = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_idphi(val) bind(C, name="set_idphi")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%idphi = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_mousedf(val) bind(C, name="set_mousedf")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%mousedf = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_mousefqso(val) bind(C, name="set_mousefqso")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%mousefqso = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nagain(val) bind(C, name="set_nagain")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nagain = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_ndepth(val) bind(C, name="set_ndepth")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ndepth = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_ndiskdat(val) bind(C, name="set_ndiskdat")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ndiskdat = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_neme(val) bind(C, name="set_neme")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%neme = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_newdat(val) bind(C, name="set_newdat")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%newdat = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nfa(val) bind(C, name="set_nfa")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nfa = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nfb(val) bind(C, name="set_nfb")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nfb = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nfcal(val) bind(C, name="set_nfcal")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nfcal = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nfshift(val) bind(C, name="set_nfshift")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nfshift = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_mcall3(val) bind(C, name="set_mcall3")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%mcall3 = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_ntimeout(val) bind(C, name="set_ntimeout")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ntimeout = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_ntol(val) bind(C, name="set_ntol")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%ntol = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nxant(val) bind(C, name="set_nxant")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nxant = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nfsample(val) bind(C, name="set_nfsample")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nfsample = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nxpol(val) bind(C, name="set_nxpol")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nxpol = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nmode(val) bind(C, name="set_nmode")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nmode = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nsave(val) bind(C, name="set_nsave")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nsave = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_max_drift(val) bind(C, name="set_max_drift")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%max_drift = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_nhsym(val) bind(C, name="set_nhsym")
    use iso_c_binding
    integer(c_int), value :: val
    call lock_decode_requests()
    draft%nhsym = val
    call unlock_decode_requests()
  end subroutine

  subroutine set_mycall(buf) bind(C, name="set_mycall")
    use iso_c_binding
    character(kind=c_char), intent(in) :: buf(12)
    integer :: i
    call lock_decode_requests()
    do i = 1, 12
      draft%mycall(i:i) = buf(i)
    end do
    call unlock_decode_requests()
  end subroutine

  subroutine set_hiscall(buf) bind(C, name="set_hiscall")
    use iso_c_binding
    character(kind=c_char), intent(in) :: buf(12)
    integer :: i
    call lock_decode_requests()
    do i = 1, 12
      draft%hiscall(i:i) = buf(i)
    end do
    call unlock_decode_requests()
  end subroutine

  subroutine set_mygrid(buf) bind(C, name="set_mygrid")
    use iso_c_binding
    character(kind=c_char), intent(in) :: buf(6)
    integer :: i
    call lock_decode_requests()
    do i = 1, 6
      draft%mygrid(i:i) = buf(i)
    end do
    call unlock_decode_requests()
  end subroutine

  subroutine set_hisgrid(buf) bind(C, name="set_hisgrid")
    use iso_c_binding
    character(kind=c_char), intent(in) :: buf(6)
    integer :: i
    call lock_decode_requests()
    do i = 1, 6
      draft%hisgrid(i:i) = buf(i)
    end do
    call unlock_decode_requests()
end subroutine

subroutine set_datetime(buf) bind(C, name="set_datetime")
  use iso_c_binding
  character(kind=c_char), intent(in) :: buf(17)
  integer :: i
    call lock_decode_requests()
  do i = 1, 17
    draft%datetime(i:i) = buf(i)
  end do
    call unlock_decode_requests()
end subroutine

  function publish_decode_request() result(request_id) bind(C)
    integer(c_int64_t) :: request_id
    type(decode_request), pointer :: request

    allocate(request)
    call lock_decode_requests()
    published_request_id = published_request_id + 1
    request_id = published_request_id
    request%id = request_id
    request%input_generation = live_input_generation
    request%parameters = draft
    request%parameters%newdat = 1
    ! Automatic passes (nhsym1/nhsym2) were just snapshotted by dataSink();
    ! remember which slot and fill they own. Manual repeats use the latest.
    if (draft%manualDecodeFlag == 0 .and. draft%nagain == 0 .and. &
        (draft%nhsym == 280 .or. draft%nhsym == 302)) then
      request%snap_idx = merge(1, 2, draft%nhsym == 280)
      request%snap_seq = snap_seq(request%snap_idx)
      ! No fill has ever happened for this slot (no snapshot support in the
      ! caller): fall back to the input-generation rule.
      if (request%snap_seq == 0) request%snap_idx = 0
    endif
    if (associated(pending_tail)) then
      pending_tail%next => request
    else
      pending_head => request
    endif
    pending_tail => request
    decoder_ready = 1
    draft%manualDecodeFlag = 0
    call unlock_decode_requests()
  end function

  logical function claim_decode_request()
    type(decode_request), pointer :: request

    call lock_decode_requests()
    claim_decode_request = decoder_ready /= 0 .and. associated(pending_head)
    if (claim_decode_request) then
      request => pending_head
      pending_head => request%next
      if (.not. associated(pending_head)) nullify(pending_tail)
      active = request%parameters
      active_request_id = request%id
      active_input_generation = request%input_generation
      active_snap_idx = 0
      if (request%snap_idx > 0) then
        ! Automatic pass: valid while its snapshot is intact, even if the
        ! minute has rolled over. A live request more than one generation old
        ! (monitoring stopped, period changed) is still dropped.
        active_request_expired = snap_valid_seq(request%snap_idx) /= request%snap_seq
        if (active%ndiskdat == 0 .and. &
            live_input_generation - request%input_generation >= 2) active_request_expired = .true.
        if (.not. active_request_expired) then
          active_snap_idx = request%snap_idx
          snap_busy(active_snap_idx) = .true.
        endif
      else
        active_request_expired = active%ndiskdat == 0 .and. active%nagain == 0 .and. &
          active%manualDecodeFlag == 0 .and. request%input_generation /= live_input_generation
        if (.not. active_request_expired .and. snap_latest_idx > 0) then
          active_snap_idx = snap_latest_idx            ! manual repeat: latest snapshot
          snap_busy(active_snap_idx) = .true.
        endif
      endif
      deallocate(request)
    endif
    call unlock_decode_requests()
  end function

  subroutine initialize_decode_parameters()
    call lock_decode_requests()
    active = draft
    call unlock_decode_requests()
  end subroutine

  ! A snapshot fill starts: invalidate the slot's contents and say whether the
  ! copy may proceed (ok = .false. when the running pass is still reading it).
  subroutine snapshot_fill_begin(idx, ok)
    integer, intent(in) :: idx
    logical, intent(out) :: ok
    call lock_decode_requests()
    snap_seq(idx) = snap_seq(idx) + 1
    snap_valid_seq(idx) = 0
    ok = .not. snap_busy(idx)
    call unlock_decode_requests()
  end subroutine

  subroutine snapshot_fill_end(idx)
    integer, intent(in) :: idx
    call lock_decode_requests()
    snap_valid_seq(idx) = snap_seq(idx)
    snap_latest_idx = idx
    call unlock_decode_requests()
  end subroutine

  ! The active pass is finished with its snapshot slot.
  subroutine snapshot_release()
    call lock_decode_requests()
    if (active_snap_idx > 0) snap_busy(active_snap_idx) = .false.
    active_snap_idx = 0
    call unlock_decode_requests()
  end subroutine

  subroutine advance_live_input_generation() bind(C)
    call lock_decode_requests()
    live_input_generation = live_input_generation + 1
    call unlock_decode_requests()
  end subroutine

  subroutine clear_pending_requests()
    type(decode_request), pointer :: request
    do while (associated(pending_head))
      request => pending_head
      pending_head => request%next
      deallocate(request)
    enddo
    nullify(pending_tail)
  end subroutine

  subroutine cancel_pending_decode_requests() bind(C)
    call lock_decode_requests()
    call clear_pending_requests()
    decoder_ready = 0
    published_request_id = published_request_id + 1
    call unlock_decode_requests()
  end subroutine

  function get_stop_m65() result(stopped)
    integer(c_int) :: stopped
    call lock_decode_requests()
    stopped = stop_m65
    call unlock_decode_requests()
  end function

  logical(c_bool) function is_current_decode_request(request_id) bind(C)
    integer(c_int64_t), value :: request_id
    call lock_decode_requests()
    is_current_decode_request = request_id == published_request_id
    call unlock_decode_requests()
  end function

end module npar_ptrs_mod