NewMadeleine

Documentation

« back to PM2 home.
nm_core_interface.h
Go to the documentation of this file.
1/*
2 * NewMadeleine
3 * Copyright (C) 2006-2026 (see AUTHORS file)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15
16
17#ifndef NM_CORE_INTERFACE_H
18#define NM_CORE_INTERFACE_H
19
24#include <Padico/Puk.h>
25#include <nm_public.h>
26#include <nm_data.h>
27#include <sys/uio.h>
28
29#ifdef PIOMAN
30#include <pioman.h>
31#else
32#include <pthread.h>
33#endif
34
59
60/* ** Core init ******************************************** */
61
62typedef struct nm_core*nm_core_t;
63
64puk_component_t nm_core_component_load(const char*entity, const char*name);
65
67
68int nm_core_set_strategy(nm_core_t p_core, puk_component_t strategy);
69
71
74
76extern struct nm_core nm_core;
77
79static inline nm_core_t nm_core_get_singleton(void)
80{
81 return &nm_core;
82}
83
86
87
88/* ** Drivers ********************************************** */
89
91typedef struct nm_drv_s*nm_drv_t;
92
94
96 nm_drv_t*pp_drv, const char**p_url);
97
98/* ** Gates ************************************************ */
99
101nm_gate_t nm_core_gate_new(nm_core_t p_core, nm_drv_vect_t*p_drvs, const struct nm_uuid_s*p_uuid);
106
107
108/* ** Threads ********************************************** */
109
119
123
128
129/* ** Progression ****************************************** */
130
132
135/* ** Status *********************************************** */
136
145#ifdef PIOMAN
147typedef piom_cond_value_t nm_status_t;
149typedef piom_cond_t nm_cond_status_t;
150#else /* PIOMAN */
152typedef uint32_t nm_status_t;
155#endif /* PIOMAN */
156
157/* ** status and flags, used in pack/unpack requests and events */
158
160#define NM_STATUS_NONE ((nm_status_t)0x00000000)
162#define NM_STATUS_PACK_INIT ((nm_status_t)0x00000001)
164#define NM_STATUS_UNPACK_INIT ((nm_status_t)0x00000002)
166#define NM_STATUS_PACK_COMPLETED ((nm_status_t)0x00000004)
168#define NM_STATUS_UNPACK_COMPLETED ((nm_status_t)0x00000008)
170#define NM_STATUS_UNEXPECTED ((nm_status_t)0x00000010)
172#define NM_STATUS_UNPACK_CANCELLED ((nm_status_t)0x00000020)
174#define NM_STATUS_PACK_POSTED ((nm_status_t)0x00000040)
176#define NM_STATUS_UNPACK_POSTED ((nm_status_t)0x00000080)
178#define NM_STATUS_ACK_RECEIVED ((nm_status_t)0x00000100)
180#define NM_STATUS_UNPACK_DATA0 ((nm_status_t)0x00000200)
182#define NM_STATUS_UNPACK_DATA_SIZE ((nm_status_t)0x00000400)
184#define NM_STATUS_FINALIZED ((nm_status_t)0x00000800)
186#define NM_STATUS_ERROR ((nm_status_t)0x00001000)
188#define NM_STATUS_PACK_MSG_SIZE ((nm_status_t)0x00002000)
190#define NM_STATUS_UNPACK_PREFETCHED ((nm_status_t)0x00004000)
191
193#define NM_STATUS_MASK_FULL ((nm_status_t)-1)
194
195
197typedef uint32_t nm_req_flag_t;
198
200#define NM_REQ_FLAG_NONE ((nm_req_flag_t)0x00000000)
202#define NM_REQ_FLAG_PACK_SYNCHRONOUS ((nm_req_flag_t)0x00001000)
204#define NM_REQ_FLAG_PACK ((nm_req_flag_t)0x00002000)
206#define NM_REQ_FLAG_UNPACK ((nm_req_flag_t)0x00004000)
208#define NM_REQ_FLAG_UNPACK_DATA_INFO ((nm_req_flag_t)0x00008000)
210#define NM_REQ_FLAG_UNPACK_MATCHING_INFO ((nm_req_flag_t)0x00010000)
212#define NM_REQ_FLAG_UNPACK_PREFETCHING ((nm_req_flag_t)0x00020000)
214#define NM_REQ_FLAG_MATCHING_WILDCARD ((nm_req_flag_t)0x00100000)
216#define NM_REQ_FLAG_MATCHING_GATE ((nm_req_flag_t)0x00200000)
218#define NM_REQ_FLAG_MATCHING_TAG ((nm_req_flag_t)0x00400000)
220#define NM_REQ_FLAG_MATCHING_FULL ((nm_req_flag_t)0x00800000)
222#define NM_REQ_FLAG_FINALIZE_LATER ((nm_req_flag_t)0x01000000)
224#define NM_REQ_FLAG_UNPACK_PARTITIONED ((nm_req_flag_t)0x02000000)
226#define NM_REQ_FLAG_PACK_PARTITIONED ((nm_req_flag_t)0x04000000)
227
229typedef uint32_t nm_req_chunk_flag_t;
230
232#define NM_REQ_CHUNK_FLAG_NONE ((nm_req_chunk_flag_t)0x00000000)
234#define NM_REQ_CHUNK_FLAG_SHORT ((nm_req_chunk_flag_t)0x00020000)
236#define NM_REQ_CHUNK_FLAG_USE_COPY ((nm_req_chunk_flag_t)0x00080000)
238#define NM_REQ_CHUNK_FLAG_DATA_ITERATOR ((nm_req_chunk_flag_t)0x00100000)
239
242/* ** tags ************************************************* */
243
253typedef uint32_t nm_session_hash_t;
254
256#define NM_CORE_TAG_HASH_FULL ((nm_session_hash_t)0xFFFFFFFF)
257
264
266
267#define NM_CORE_TAG_MASK_FULL ((nm_core_tag_t){ .tag = NM_TAG_MASK_FULL, .hashcode = NM_CORE_TAG_HASH_FULL })
268#define NM_CORE_TAG_NONE ((nm_core_tag_t){ .tag = 0, .hashcode = 0x0 })
269
271{
272 nm_core_tag_t core_tag;
273 core_tag.tag = tag;
274 core_tag.hashcode = hashcode;
275 return core_tag;
276}
278{
279 return core_tag.tag;
280}
282{
283 return core_tag.hashcode;
284}
285
289/* ** Event notification *********************************** */
290
309
311typedef void (*nm_core_event_notifier_t)(const struct nm_core_event_s*const event, void*ref);
312
320
328
340void nm_core_req_monitor(struct nm_core*p_core, struct nm_req_s*p_req, struct nm_monitor_s monitor);
341
342
344#define NM_EVENT_MATCHING_ANY ((struct nm_core_event_matching_s){ .p_gate = NM_ANY_GATE, .tag = NM_CORE_TAG_NONE, .tag_mask = NM_CORE_TAG_NONE })
345
346#define NM_MONITOR_NULL ((struct nm_monitor_s){ .p_notifier = NULL, .event_mask = 0, .ref = NULL })
347
348#define NM_CORE_MONITOR_NULL ((struct nm_core_monitor_s){ .monitor = NM_MONITOR_NULL, .matching = NM_EVENT_MATCHING_ANY })
349
352/* ** Core tasks ******************************************* */
353
377
386#define NM_MATCHING_CONTAINER_NULL ((struct nm_matching_container_s) { NULL })
387
400{
402 union
403 {
404 struct
405 {
409 struct
410 {
413 struct
414 {
415 struct nm_pkt_wrap_s*p_pw;
417 struct
418 {
419 struct nm_pkt_wrap_s*p_pw;
421 struct
422 {
425 struct
426 {
427 struct nm_pkt_wrap_s*p_pw;
429 struct
430 {
431 void (*p_handler)(void);
434};
435
439void nm_core_task_submit_locked(struct nm_core*p_core, void (*p_handler)(void));
440
444void nm_core_task_submit_unlocked(struct nm_core*p_core, void (*p_handler)(void));
445
448/* ** pollable objects ************************************* */
449
450typedef int (*nm_core_poll_func_t)(void*p_poll_ref);
451
452PUK_LIST_DECLARE_TYPE(nm_core_pollable);
459PUK_LIST_CREATE_FUNCS(nm_core_pollable);
460
461/* ** Packs/unpacks **************************************** */
462
476
478
490
492
539
541void nm_core_pack_init(struct nm_core*p_core, struct nm_req_s*p_pack);
542
544void nm_core_pack_data(nm_core_t p_core, struct nm_req_s*p_pack, const struct nm_data_s*p_data);
545
548
550void nm_core_pack_submit(struct nm_core*p_core, struct nm_req_s*p_pack);
551
553void nm_core_pack_set_priority(struct nm_core*p_core, struct nm_req_s*p_pack, nm_prio_t priority);
554
556static inline void nm_core_pack_set_hlen(struct nm_core*p_core __attribute__((unused)), struct nm_req_s*p_pack, nm_len_t hlen)
557{
558 p_pack->pack.hlen = hlen;
559}
560
561void nm_core_pack_submit_chunks(struct nm_core*p_core, struct nm_req_s*p_pack, int n, const struct nm_chunk_s*p_chunks);
562
564void nm_core_unpack_init(struct nm_core*p_core, struct nm_req_s*p_unpack);
565
567void nm_core_unpack_offset(struct nm_core*p_core, struct nm_req_s*p_unpack, nm_len_t offset);
568
570void nm_core_unpack_data(struct nm_core*p_core, struct nm_req_s*p_unpack, const struct nm_data_s*p_data);
571
574
576void nm_core_unpack_match_event(struct nm_core*p_core, struct nm_req_s*p_unpack, const struct nm_core_event_s*p_event);
577
579void nm_core_unpack_submit(struct nm_core*p_core, struct nm_req_s*p_unpack, nm_req_flag_t flags);
580
582int nm_core_unpack_peek(struct nm_core*p_core, struct nm_req_s*p_unpack, const struct nm_data_s*p_data,
583 nm_len_t peek_offset, nm_len_t peek_len);
584
587int nm_core_unpack_iprobe(struct nm_core*p_core, struct nm_req_s*p_unpack);
588
592int nm_core_unpack_cancel(struct nm_core*p_core, struct nm_req_s*p_unpack);
593
595int nm_core_iprobe(struct nm_core*p_core,
597 nm_gate_t*pp_out_gate, nm_core_tag_t*p_out_tag, nm_len_t*p_out_size);
598
600void nm_core_flush(struct nm_core*p_core);
601
602
603/* ** Packet injection from outside of nmad core */
604
607
614typedef void(*nm_injector_pull_data_t)(struct nm_req_s*p_req, const struct nm_data_s*p_data, nm_len_t chunk_offset, nm_len_t chunk_len, void*p_ref);
615
619 nm_len_t chunk_offset, nm_len_t chunk_len, int is_last_chunk,
620 nm_injector_pull_data_t p_pull_data, void*p_ref);
621
624
626void nm_core_inject_finalize(struct nm_core*p_core, struct nm_req_s*p_req);
627
630
631/* ** partitioned unpack */
632
633void nm_core_unpack_partition_set(struct nm_req_s*p_unpack, int n_partitions);
634
636
637int nm_core_unpack_partition_test(struct nm_req_s*p_unpack, int partition);
638
642/* ** synchronization primitives *************************** */
643
657static inline void nm_cond_init(nm_cond_status_t*p_cond, nm_status_t bitmask);
658
660static inline void nm_cond_destroy(nm_cond_status_t*p_cond);
661
663static inline nm_status_t nm_cond_test(const nm_cond_status_t*p_cond, nm_status_t bitmask);
664
666static inline nm_status_t nm_cond_test_locked(const nm_cond_status_t*p_cond, nm_status_t bitmask);
667
670static inline void nm_cond_add(nm_cond_status_t*p_cond, nm_status_t bitmask);
671
673static inline void nm_cond_wait(nm_cond_status_t*p_cond, nm_status_t bitmask, nm_core_t p_core);
674
676static inline void nm_cond_signal(nm_cond_status_t*p_cond, nm_status_t bitmask);
677
679static inline void nm_cond_wait_all(void**pp_conds, int n, uintptr_t offset, nm_status_t bitmask, nm_core_t p_core);
680
681#if defined(PIOMAN)
682
684static inline void nm_cond_init(nm_cond_status_t*p_cond, nm_status_t bitmask)
685{
686 piom_cond_init(p_cond, bitmask);
687}
689static inline void nm_cond_destroy(nm_cond_status_t*p_cond)
690{
691 piom_cond_destroy(p_cond);
692}
694static inline nm_status_t nm_cond_test(const nm_cond_status_t*p_cond, nm_status_t bitmask)
695{
696 return piom_cond_test(p_cond, bitmask);
697}
699static inline nm_status_t nm_cond_test_locked(const nm_cond_status_t*p_cond, nm_status_t bitmask)
700{
701 return piom_cond_test_locked((nm_cond_status_t*)p_cond, bitmask);
702}
704static inline void nm_cond_add(nm_cond_status_t*p_cond, nm_status_t bitmask)
705{
706 piom_cond_add(p_cond, bitmask);
707}
709static inline void nm_cond_mask(nm_cond_status_t*p_cond, nm_status_t bitmask)
710{
711 piom_cond_mask(p_cond, bitmask);
712}
714static inline void nm_cond_wait(nm_cond_status_t*p_cond, nm_status_t bitmask, nm_core_t p_core __attribute__((unused)))
715{
716 piom_cond_wait(p_cond, bitmask);
717}
719static inline void nm_cond_signal(nm_cond_status_t*p_cond, nm_status_t bitmask)
720{
721 piom_cond_signal(p_cond, bitmask);
722}
724static inline void nm_cond_wait_all(void**pp_conds, int n, uintptr_t offset, nm_status_t bitmask, nm_core_t p_core __attribute__((unused)))
725{
726 piom_cond_wait_all(pp_conds, n, offset, bitmask);
727}
728#else /* PIOMAN */
730static inline void nm_cond_init(nm_cond_status_t*p_cond, nm_status_t bitmask)
731{
732 *p_cond = bitmask;
733}
735static inline void nm_cond_destroy(nm_cond_status_t*p_cond __attribute__((unused)))
736{
737}
739static inline nm_status_t nm_cond_test(const nm_cond_status_t*p_cond, nm_status_t bitmask)
740{
741 return ((*p_cond) & bitmask);
742}
745{
746 return nm_cond_test(p_cond, bitmask);
747}
749static inline void nm_cond_add(nm_cond_status_t*p_cond, nm_status_t bitmask)
750{
751 *p_cond |= bitmask;
752}
754static inline void nm_cond_mask(nm_cond_status_t*p_cond, nm_status_t bitmask)
755{
756 *p_cond &= ~bitmask;
757}
759static inline void nm_cond_signal(nm_cond_status_t*p_cond, nm_status_t bitmask)
760{
761 *p_cond |= bitmask;
762}
764static inline void nm_cond_wait(nm_cond_status_t*p_cond, nm_status_t bitmask, nm_core_t p_core)
765{
766 while(!nm_cond_test(p_cond, bitmask))
767 {
768 nm_schedule(p_core);
769 }
770}
772static inline void nm_cond_wait_all(void**pp_conds, int n, uintptr_t offset, nm_status_t bitmask, nm_core_t p_core)
773{
774 int i;
775 for(i = 0; i < n; i++)
776 {
777 if(pp_conds[i] != NULL)
778 {
779 nm_cond_status_t*p_cond = (nm_cond_status_t*)((uintptr_t)pp_conds[i] + offset);
780 nm_cond_wait(p_cond, bitmask, p_core);
781 }
782 }
783}
784#endif /* PIOMAN */
785
788/* ** convenient frontends to deal with status in requests */
789
799static inline void nm_status_init(struct nm_req_s*p_req, nm_status_t bitmask)
800{
801 nm_cond_init(&p_req->status, bitmask);
802}
803static inline void nm_status_destroy(struct nm_req_s*p_req)
804{
805 nm_cond_destroy(&p_req->status);
806}
808static inline nm_status_t nm_status_test(const struct nm_req_s*p_req, nm_status_t bitmask)
809{
810 if(bitmask & NM_STATUS_FINALIZED) /* status FINALIZED needs strong consistency to avoid use after free */
811 return nm_cond_test_locked(&p_req->status, bitmask);
812 else
813 return nm_cond_test(&p_req->status, bitmask);
814}
816static inline void nm_status_add(struct nm_req_s*p_req, nm_status_t bitmask)
817{
818 nm_cond_add(&p_req->status, bitmask);
819}
821static inline void nm_status_unset(struct nm_req_s*p_req, nm_status_t bitmask)
822{
823 nm_cond_mask(&p_req->status, bitmask);
824}
826static inline void nm_status_wait(struct nm_req_s*p_req, nm_status_t bitmask, nm_core_t p_core)
827{
828 nm_cond_wait(&p_req->status, bitmask, p_core);
829 assert(nm_status_test(p_req, bitmask) != 0);
830}
835static inline void nm_status_signal(struct nm_req_s*p_req, nm_status_t bitmask)
836{
837 nm_cond_signal(&p_req->status, bitmask);
838}
840static inline void nm_status_wait_all(void**pp_reqs, int n, uintptr_t offset,
841 nm_status_t bitmask, nm_core_t p_core)
842{
843 const struct nm_req_s*p_req = NULL;
844 const uintptr_t status_offset = (uintptr_t)&p_req->status - (uintptr_t)p_req; /* offset of 'status' in nm_req_s */
845 nm_cond_wait_all(pp_reqs, n, offset + status_offset, bitmask, p_core);
846}
847
848#define nm_status_assert(p_req, value) \
849 assert(nm_status_test((p_req), NM_STATUS_MASK_FULL) == (value))
850
851static inline void nm_status_spinwait(struct nm_req_s*p_req, nm_status_t status)
852{
853 while(!nm_status_test(p_req, status))
854 { /* bust wait*/ }
855}
857static inline int nm_status_test_allbits(struct nm_req_s*p_req, nm_status_t bitmask)
858{
859 return (nm_status_test(p_req, bitmask) == bitmask);
860}
861
864/* ** frontends for atomic ops ***************************** */
865
876static inline void nm_mem_fence_always(void)
877{
878 __sync_synchronize();
879}
880
882static inline void nm_mem_fence(void)
883{
884#if defined(PIOMAN_MULTITHREAD)
885 __sync_synchronize();
886#else
887 nm_core_t p_core = nm_core_get_singleton();
889 {
890 __sync_synchronize();
891 }
892#endif /* PIOMAN_MULTITHREAD */
893}
894
896static inline int nm_atomic_inc(int*v)
897{
898#if defined(PIOMAN_MULTITHREAD)
899 return __sync_fetch_and_add(v, 1);
900#else
901 nm_core_t p_core = nm_core_get_singleton();
903 {
904 return __sync_fetch_and_add(v, 1);
905 }
906 else
907 {
908 return (*v)++;
909 }
910#endif /* PIOMAN_MULTITHREAD */
911}
912
914static inline int nm_atomic_always_inc(int*v)
915{
916 return __sync_fetch_and_add(v, 1);
917}
918
920static inline int nm_atomic_dec(int*v)
921{
922#if defined(PIOMAN_MULTITHREAD)
923 return __sync_sub_and_fetch(v, 1);
924#else
925 nm_core_t p_core = nm_core_get_singleton();
927 {
928 return __sync_sub_and_fetch(v, 1);
929 }
930 else
931 {
932 return --(*v);
933 }
934#endif /* PIOMAN_MULTITHREAD */
935}
936
938static inline int nm_atomic_always_dec(int*v)
939{
940 return __sync_sub_and_fetch(v, 1);
941}
942
944static inline void nm_atomic_add(int*v, int v2)
945{
946#if defined(PIOMAN_MULTITHREAD)
947 __sync_fetch_and_add(v, v2);
948#else
949 nm_core_t p_core = nm_core_get_singleton();
951 {
952 __sync_fetch_and_add(v, v2);
953 }
954 else
955 {
956 (*v) += v2;
957 }
958#endif /* PIOMAN_MULTITHREAD */
959}
960
962static inline void nm_atomic_always_add(int*v, int v2)
963{
964 __sync_fetch_and_add(v, v2);
965}
966
968static inline int nm_atomic_compare_and_swap(int*v, int oldval, int newval)
969{
970#if defined(PIOMAN_MULTITHREAD)
971 return __sync_bool_compare_and_swap(v, oldval, newval);
972#else
973 nm_core_t p_core = nm_core_get_singleton();
975 {
976 return __sync_bool_compare_and_swap(v, oldval, newval);
977 }
978 else
979 {
980 if(*v == oldval)
981 {
982 *v = newval;
983 return 1;
984 }
985 else
986 {
987 return 0;
988 }
989 }
990#endif /* PIOMAN_MULTITHREAD */
991}
992
994static inline int nm_atomic_always_compare_and_swap(int*v, int oldval, int newval)
995{
996 return __sync_bool_compare_and_swap(v, oldval, newval);
997}
998
1001/* ** frontend for generic locking ************************* */
1002
1019#ifdef PIOMAN
1020typedef piom_spinlock_t nm_spinlock_t;
1021#else /* PIOMAN */
1023{
1024#ifdef NMAD_DEBUG
1025 int lock;
1026 pthread_t last_tid;
1027#endif /* NMAD_DEBUG */
1028};
1030#endif /* PIOMAN */
1031
1033static inline void nm_spin_init(nm_spinlock_t*p_spin);
1034
1036static inline void nm_spin_destroy(nm_spinlock_t*p_spin);
1037
1039static inline void nm_spin_lock(nm_spinlock_t*p_spin);
1040
1042static inline void nm_spin_unlock(nm_spinlock_t*p_spin);
1043
1047static inline int nm_spin_trylock(nm_spinlock_t*p_spin);
1048
1050static inline void nm_spin_assert_locked(nm_spinlock_t*p_spin);
1051
1053static inline void nm_spin_assert_notlocked(nm_spinlock_t*p_spin);
1054
1055
1057static inline void nm_spin_check_nothread(nm_spinlock_t*p_spin __attribute__((unused)))
1058{
1059#if defined(NMAD_DEBUG) && !defined(PIOMAN)
1060 nm_core_t p_core = nm_core_get_singleton();
1062 if(p_spin->last_tid == (pthread_t)0)
1063 {
1064 p_spin->last_tid = pthread_self();
1065 __sync_synchronize();
1066 }
1067 else
1068 {
1069 if(p_spin->last_tid != pthread_self())
1070 {
1071 NM_FATAL("detected calls from multiple threads in non-threaded mode. Please use pioman-enabled build for multi-threaded use or give thread level using nm_core_set_thread_level(NM_THREAD_SERIALIZED) for serialized thread level.");
1072 }
1073 }
1074#endif /* NMAD_DEBUG && !PIOMAN */
1075}
1076
1078static inline void nm_spin_clear_nothread(nm_spinlock_t*p_spin __attribute__((unused)))
1079{
1080#if defined(NMAD_DEBUG) && !defined(PIOMAN)
1081 nm_core_t p_core = nm_core_get_singleton();
1083 if(p_spin->last_tid == 0)
1084 {
1085 NM_FATAL("unlocking while no thread is holding the lock.");
1086 }
1087 else if(p_spin->last_tid != pthread_self())
1088 {
1089 NM_WARN("unlocking from another thread than where lock was acquired.\n");
1090 }
1092 {
1093 p_spin->last_tid = (pthread_t)0;
1094 }
1095#endif /* NMAD_DEBUG && !PIOMAN */
1096}
1097
1098static inline void nm_spin_init(nm_spinlock_t*p_spin __attribute__((unused)))
1099{
1100#ifdef PIOMAN
1101 piom_spin_init(p_spin);
1102#else /* PIOMAN */
1103#ifdef NMAD_DEBUG
1104 p_spin->lock = 0;
1105 p_spin->last_tid = 0;
1106#endif /* NMAD_DEBUG */
1107#endif /* PIOMAN */
1108}
1109
1110static inline void nm_spin_destroy(nm_spinlock_t*p_spin __attribute__((unused)))
1111{
1112#ifdef PIOMAN
1113 piom_spin_destroy(p_spin);
1114#else /* PIOMAN */
1115#ifdef NMAD_DEBUG
1116 assert(p_spin->lock == 0);
1117#endif /* NMAD_DEBUG */
1118#endif /* PIOMAN */
1119}
1120
1121static inline void nm_spin_lock(nm_spinlock_t*p_spin __attribute__((unused)))
1122{
1123#ifdef PIOMAN
1124 piom_spin_lock(p_spin);
1125#else /* PIOMAN */
1126#ifdef NMAD_DEBUG
1127 __sync_synchronize();
1128 if(p_spin->lock != 0)
1129 {
1130 NM_FATAL("spinlock is not free in nm_spin_lock(); detected concurrent access from thread = %p. Suspecting multi-threaded use by the application while library is initialized in non-threaded mode.\n",
1131 (void*)p_spin->last_tid);
1132 }
1133 p_spin->lock = 1;
1134#endif /* NMAD_DEBUG */
1135 nm_spin_check_nothread(p_spin);
1136 nm_core_t p_core = nm_core_get_singleton();
1138 {
1139 __sync_synchronize();
1140 }
1141#endif /* PIOMAN */
1142}
1143
1144static inline void nm_spin_unlock(nm_spinlock_t*p_spin __attribute__((unused)))
1145{
1146#ifdef PIOMAN
1147 piom_spin_unlock(p_spin);
1148#else /* PIOMAN */
1149 nm_spin_clear_nothread(p_spin);
1150#ifdef NMAD_DEBUG
1151 __sync_synchronize();
1152 assert(p_spin->lock == 1);
1153 p_spin->lock = 0;
1154#endif /* NMAD_DEBUG */
1155 nm_core_t p_core = nm_core_get_singleton();
1157 {
1158 __sync_synchronize();
1159 }
1160#endif /* PIOMAN */
1161}
1162
1163static inline int nm_spin_trylock(nm_spinlock_t*p_spin __attribute__((unused)))
1164{
1165#ifdef PIOMAN
1166 return piom_spin_trylock(p_spin);
1167#else /* PIOMAN */
1168 int rc = 1;
1169#ifdef NMAD_DEBUG
1170 __sync_synchronize();
1171 if(p_spin->lock)
1172 {
1173 assert(p_spin->lock == 1);
1174 rc = 0;
1175 }
1176 else
1177 {
1178 rc = 1;
1179 p_spin->lock = 1;
1180 nm_spin_check_nothread(p_spin);
1181 }
1182#endif /* NMAD_DEBUG */
1183 nm_core_t p_core = nm_core_get_singleton();
1185 {
1186 __sync_synchronize();
1187 }
1188 return rc;
1189#endif /* PIOMAN */
1190}
1191
1192static inline void nm_spin_assert_locked(nm_spinlock_t*p_spin __attribute__((unused)))
1193{
1194#ifdef PIOMAN
1195 piom_spin_assert_locked(p_spin);
1196#else /* PIOMAN */
1197#ifdef NMAD_DEBUG
1198 assert(p_spin->lock == 1);
1199 assert(p_spin->last_tid == pthread_self());
1200#endif /* NMAD_DEBUG */
1201#endif /* PIOMAN */
1202}
1203
1204static inline void nm_spin_assert_notlocked(nm_spinlock_t*p_spin __attribute__((unused)))
1205{
1206#ifdef PIOMAN
1207 piom_spin_assert_notlocked(p_spin);
1208#else /* PIOMAN */
1209#ifdef NMAD_DEBUG
1210 assert(p_spin->lock == 0);
1211#endif /* NMAD_DEBUG */
1212#endif /* PIOMAN */
1213}
1214
1216
1220#endif /* NM_CORE_INTERFACE_H */
static int nm_atomic_compare_and_swap(int *v, int oldval, int newval)
boolean int compare and swap, atomic only when multithread
static void nm_atomic_always_add(int *v, int v2)
int add, always atomic
static int nm_atomic_dec(int *v)
decrement int, atomic only when multithread
static int nm_atomic_always_dec(int *v)
decrement int, always atomic
static int nm_atomic_always_inc(int *v)
increment int, always atomic
static int nm_atomic_inc(int *v)
increment int, atomic only when multithread
static void nm_mem_fence_always(void)
memory fence, always
static int nm_atomic_always_compare_and_swap(int *v, int oldval, int newval)
boolean int compare and swap, always atomic
static void nm_mem_fence(void)
memory fence only when multithread
static void nm_atomic_add(int *v, int v2)
int add, atomic only when multithread
static void nm_cond_init(nm_cond_status_t *p_cond, nm_status_t bitmask)
initialize a nm_cond_status_t object
static nm_status_t nm_cond_test(const nm_cond_status_t *p_cond, nm_status_t bitmask)
test whether the given bit is set in the status; unlocked, weak consistency
static void nm_cond_add(nm_cond_status_t *p_cond, nm_status_t bitmask)
add a bit to the bitmask in the status, do not unlock waiters (for bits that will not be waited for)
static void nm_cond_wait_all(void **pp_conds, int n, uintptr_t offset, nm_status_t bitmask, nm_core_t p_core)
wait on multiple statuses at the same time
static void nm_cond_signal(nm_cond_status_t *p_cond, nm_status_t bitmask)
add a bit and wake up threads waiting for it
static nm_status_t nm_cond_test_locked(const nm_cond_status_t *p_cond, nm_status_t bitmask)
test whether the given bit is set in the status; locked, guaranteed consistency, slower
static void nm_cond_wait(nm_cond_status_t *p_cond, nm_status_t bitmask, nm_core_t p_core)
wait for the given bit to be set in the status; do active polling while waiting
static void nm_cond_mask(nm_cond_status_t *p_cond, nm_status_t bitmask)
static void nm_cond_destroy(nm_cond_status_t *p_cond)
free resources associated with a nm_cond_status_t object
void nm_core_monitor_add(nm_core_t p_core, struct nm_core_monitor_s *m)
Register an event monitor.
void(* nm_core_event_notifier_t)(const struct nm_core_event_s *const event, void *ref)
an event notifier, fired upon status transition
struct nm_core_event_s __attribute__
Definition nm_data.h:539
void nm_core_req_monitor(struct nm_core *p_core, struct nm_req_s *p_req, struct nm_monitor_s monitor)
set a per-request monitor.
void nm_core_monitor_remove(nm_core_t p_core, struct nm_core_monitor_s *m)
Unregister an event monitor.
int nm_core_driver_load_init(nm_core_t p_core, puk_component_t driver, nm_trk_kind_t kind, nm_drv_t *pp_drv, const char **p_url)
puk_component_t nm_core_component_load(const char *entity, const char *name)
void nm_core_schedopt_disable(nm_core_t p_core)
disable schedopt for raw driver use
typedefNM_EXTERN_C_BEGIN struct nm_core * nm_core_t
int nm_schedule(nm_core_t p_core)
nm_gate_t nm_core_gate_new(nm_core_t p_core, nm_drv_vect_t *p_drvs, const struct nm_uuid_s *p_uuid)
Init a new gate, using the given set of drivers.
nm_thread_level_e
struct nm_drv_s * nm_drv_t
a nmad driver; opaque type for the user
int nm_core_exit(nm_core_t p_core)
nm_thread_level_t nm_core_get_thread_level(nm_core_t)
Get the current thread level.
enum nm_thread_level_e nm_thread_level_t
int nm_core_set_strategy(nm_core_t p_core, puk_component_t strategy)
PUK_VECT_TYPE(nm_drv, nm_drv_t)
struct nm_core nm_core
exposed here for inlining; do not use this value, use the accessor nm_core_get_singleton()
void nm_trace_add_synchro_point(void)
generate a synchronization event to synchronize nmad traces with others (e.g.
void nm_core_set_thread_level(nm_thread_level_t)
Sets the thread level before nm core init.
int nm_core_init(nm_core_t *pp_core)
void nm_core_gate_connect_wait(nm_core_t p_core, struct nm_trk_s *p_trk)
wait for connection completion
void nm_core_gate_connect_async(nm_core_t p_core, nm_gate_t gate, nm_drv_t p_drv, nm_trk_id_t trk_id, const char *url)
start connection process on given gate/trk
@ NM_THREAD_FUNNELED
@ NM_THREAD_SINGLE
@ NM_THREAD_MULTIPLE
@ NM_THREAD_UNKNOWN
@ NM_THREAD_SERIALIZED
int nm_core_unpack_iprobe(struct nm_core *p_core, struct nm_req_s *p_unpack)
probes whether an incoming packet matched this unposted request.
void nm_core_inject_finalize(struct nm_core *p_core, struct nm_req_s *p_req)
finalize an injected request that was only completed.
int nm_core_unpack_cancel(struct nm_core *p_core, struct nm_req_s *p_unpack)
cancel a pending unpack
void nm_core_pack_data(nm_core_t p_core, struct nm_req_s *p_pack, const struct nm_data_s *p_data)
build a pack request from data descriptor
void nm_core_unpack_init(struct nm_core *p_core, struct nm_req_s *p_unpack)
initializes an empty unpack request
int nm_core_unpack_peek(struct nm_core *p_core, struct nm_req_s *p_unpack, const struct nm_data_s *p_data, nm_len_t peek_offset, nm_len_t peek_len)
peeks unexpected data without consumming it.
int nm_core_unpack_partition_test(struct nm_req_s *p_unpack, int partition)
void nm_core_inject_chunk(struct nm_core *p_core, nm_gate_t p_gate, nm_core_tag_t tag, nm_seq_t seq, nm_len_t chunk_offset, nm_len_t chunk_len, int is_last_chunk, nm_injector_pull_data_t p_pull_data, void *p_ref)
inject a packet in nmad core as if it arrived from network.
int nm_core_iprobe(struct nm_core *p_core, nm_gate_t p_gate, nm_core_tag_t tag, nm_core_tag_t tag_mask, nm_gate_t *pp_out_gate, nm_core_tag_t *p_out_tag, nm_len_t *p_out_size)
probe unexpected packet, check matching for (packet_tag & tag_mask) == tag
void nm_core_pack_send(struct nm_core *p_core, struct nm_req_s *p_pack, nm_core_tag_t tag, nm_gate_t p_gate, nm_req_flag_t flags)
set tag/gate/flags for pack request
void(* nm_injector_pull_data_t)(struct nm_req_s *p_req, const struct nm_data_s *p_data, nm_len_t chunk_offset, nm_len_t chunk_len, void *p_ref)
user-supplied function called to pull data to posted request through nmad core p_req is the user requ...
void nm_core_unpack_offset(struct nm_core *p_core, struct nm_req_s *p_unpack, nm_len_t offset)
set an offset on data; data before offset will be discarded
void nm_core_unpack_match_recv(struct nm_core *p_core, struct nm_req_s *p_unpack, nm_gate_t p_gate, nm_core_tag_t tag, nm_core_tag_t tag_mask)
match an unpack request with given gate/tag, next sequence number assumed
void nm_core_pack_init(struct nm_core *p_core, struct nm_req_s *p_pack)
initializes an empty pack request
void nm_core_inject_complete_finalize(struct nm_core *p_core, struct nm_req_s *p_req, nm_len_t chunk_offset, nm_len_t chunk_len)
notify data was injected in a matched request and finalize this request.
void nm_core_unpack_partition_set(struct nm_req_s *p_unpack, int n_partitions)
void nm_core_pack_submit(struct nm_core *p_core, struct nm_req_s *p_pack)
post a pack request
void nm_core_inject_complete(struct nm_core *p_core, struct nm_req_s *p_req, nm_len_t chunk_offset, nm_len_t chunk_len)
notify data was injected in a matched request, but do not finalize the request (the status will be NM...
void nm_core_unpack_data(struct nm_core *p_core, struct nm_req_s *p_unpack, const struct nm_data_s *p_data)
build an unpack request from data descriptor
void nm_core_flush(struct nm_core *p_core)
Flush pending packs (if supported by the strategy).
static void nm_core_pack_set_hlen(struct nm_core *p_core __attribute__((unused)), struct nm_req_s *p_pack, nm_len_t hlen)
set a header length for the given pack request
void nm_core_unpack_match_event(struct nm_core *p_core, struct nm_req_s *p_unpack, const struct nm_core_event_s *p_event)
match an unpack request with a packet that triggered an event
void nm_core_unpack_partition_free(struct nm_req_s *p_unpack)
void nm_core_unpack_submit(struct nm_core *p_core, struct nm_req_s *p_unpack, nm_req_flag_t flags)
submit an unpack request
void nm_core_pack_set_priority(struct nm_core *p_core, struct nm_req_s *p_pack, nm_prio_t priority)
set a priority for the given pack request
nm_seq_t nm_core_send_seq_get(struct nm_core *p_core, nm_gate_t p_gate, nm_core_tag_t tag)
get a seq number in the out stream, to route packet outside of nmad core
void nm_core_pack_submit_chunks(struct nm_core *p_core, struct nm_req_s *p_pack, int n, const struct nm_chunk_s *p_chunks)
static nm_session_hash_t nm_core_tag_get_hashcode(nm_core_tag_t core_tag)
uint32_t nm_session_hash_t
a session hashcode in tags, used to multiplex sessions
static nm_core_tag_t nm_core_tag_build(nm_session_hash_t hashcode, nm_tag_t tag)
static nm_tag_t nm_core_tag_get_tag(nm_core_tag_t core_tag)
nm_core_task_kind_e
enum nm_core_task_kind_e nm_core_task_kind_t
void nm_core_task_submit_unlocked(struct nm_core *p_core, void(*p_handler)(void))
submit task lock-free to the submission list This is used mostly for benchmarks.
void nm_core_task_submit_locked(struct nm_core *p_core, void(*p_handler)(void))
lock then submit task to pending list This is used mostly for benchmarks.
@ NM_CORE_TASK_COMPLETED_PREFETCH
prefetch completed; process RTR if received
@ NM_CORE_TASK_UNPACK_NEXT
try to match the next unpack on the given gate/tag/gtag
@ NM_CORE_TASK_RTR_SEND
send a RTR once the large pw for recv has been posted
@ NM_CORE_TASK_PACK_SUBMISSION
process a submitted pack request
@ NM_CORE_TASK_NONE
@ NM_CORE_TASK_NOP
a core task that does nothing (but is not invalid nor uninitialized)
@ NM_CORE_TASK_COMPLETED_PW
process a completed pw
@ NM_CORE_TASK_CANCELLED_PREFETCH
prefetch cancelled; release ref on pw
@ NM_CORE_TASK_HANDLER
call a user handler, mainly for testing/benchmarking
@ NM_CORE_TASK_INVALID
static void nm_spin_check_nothread(nm_spinlock_t *p_spin __attribute__((unused)))
check that we are always called from the same thread in case of non-threaded mode
static void nm_spin_init(nm_spinlock_t *p_spin)
init the spin lock
static void nm_spin_assert_notlocked(nm_spinlock_t *p_spin)
assert that current thread doesn't hold the lock
static int nm_spin_trylock(nm_spinlock_t *p_spin)
try to lock the spin lock return 1 if lock is successfully acquired, 0 otherwise
static void nm_spin_clear_nothread(nm_spinlock_t *p_spin __attribute__((unused)))
clear the last_tid tracking for lock consistency checking
static void nm_spin_destroy(nm_spinlock_t *p_spin)
destroy the spin lock
struct nm_spinlock_s nm_spinlock_t
static void nm_spin_lock(nm_spinlock_t *p_spin)
acquire the spin lock
static void nm_spin_assert_locked(nm_spinlock_t *p_spin)
assert that current thread holds the lock
static void nm_spin_unlock(nm_spinlock_t *p_spin)
release the spin lock
static void nm_status_signal(struct nm_req_s *p_req, nm_status_t bitmask)
add the bits from bitmak to the status and wakes-up all others waiting on nm_status_wait().
static void nm_status_wait_all(void **pp_reqs, int n, uintptr_t offset, nm_status_t bitmask, nm_core_t p_core)
wait for all reqs, any bit in bitmask
static void nm_status_unset(struct nm_req_s *p_req, nm_status_t bitmask)
remove bits of bitmask from req status
static void nm_status_spinwait(struct nm_req_s *p_req, nm_status_t status)
static void nm_status_init(struct nm_req_s *p_req, nm_status_t bitmask)
initialize cond status with given initial value
static int nm_status_test_allbits(struct nm_req_s *p_req, nm_status_t bitmask)
tests for all given bits in status
static void nm_status_destroy(struct nm_req_s *p_req)
static void nm_status_add(struct nm_req_s *p_req, nm_status_t bitmask)
add a bit to the status of the request; does not unlock others (no signal)
static void nm_status_wait(struct nm_req_s *p_req, nm_status_t bitmask, nm_core_t p_core)
wait for any bit matching in req status
static nm_status_t nm_status_test(const struct nm_req_s *p_req, nm_status_t bitmask)
query for given bits in req status; returns matched bits
nm_status_t nm_cond_status_t
status with synchronization (wait/signal)
uint32_t nm_req_flag_t
pack/unpack flags
#define NM_STATUS_FINALIZED
request is finalized, may be freed
uint32_t nm_status_t
status bits of pack/unpack requests
uint32_t nm_req_chunk_flag_t
flags for req_chunk
PUK_LIST_CREATE_FUNCS(nm_core_pollable)
PUK_LIST_DECLARE_TYPE(nm_core_pollable)
int(* nm_core_poll_func_t)(void *p_poll_ref)
nm_session_hash_t hashcode
the session hashcode
nm_tag_t tag
the user-supplied tag
High-level data manipulation through iterators.
assert(p_data->ops.p_traversal !=NULL)
nm_data_propertie_gpu_preinit & p_data
Definition nm_data.h:539
static nm_gate_t p_gate
nm_prio_t priority
Definition nm_headers.h:6
uint16_t hlen
length in header (header + data in header)
Definition nm_headers.h:5
nm_len_t chunk_len
length of this chunk
Definition nm_headers.h:4
nm_trk_id_t trk_id
index of the track relative to the gate
Definition nm_headers.h:3
nm_len_t chunk_offset
offset of the enclosed chunk
Definition nm_headers.h:4
nm_seq_t seq
sequence number
Definition nm_headers.h:2
nm_gate_t gate
gate of the destination or the source node
nm_mpi_status_t status
status of request
nm_onesided_flag_t flags
This is the common public header for NewMad.
#define NM_EXTERN_C_END
Definition nm_public.h:78
#define NM_EXTERN_C_BEGIN
Definition nm_public.h:77
#define NM_FATAL(format,...)
Definition nm_public.h:67
#define NM_WARN(format,...)
Definition nm_public.h:65
int8_t nm_trk_id_t
ID of a track, assigned in order.
Definition nm_types.h:92
uint64_t nm_req_seq_t
sequence number for requests
Definition nm_types.h:117
int32_t nm_prio_t
message priority
Definition nm_types.h:78
uint64_t nm_tag_t
user tags, 64 bits, contained in indirect hashtable
Definition nm_types.h:56
uint64_t nm_len_t
data length used by nmad
Definition nm_types.h:68
uint32_t nm_seq_t
Sequence number for packets on a given gate/tag.
Definition nm_types.h:108
uint8_t nm_proto_t
protocol flags- not part of the public API, but needed for inline
Definition nm_types.h:105
enum nm_trk_kind_e nm_trk_kind_t
nm_len_t chunk_offset
matching info for global monitors
nm_core_tag_t tag_mask
the mask to apply before comparing tags (only bits set in mask will be checked)
nm_core_tag_t tag
the tag to listen too
nm_gate_t p_gate
the gate to listen to, or NM_ANY_GATE for any
An event, generated by the NewMad core.
struct nm_req_s * p_req
the request that matched the event- NULL in case of unexpected packets
nm_status_t status
status flags- describe the event
global monitor for status transitions
struct nm_monitor_s monitor
the monitor to fire upon matching event
struct nm_core_event_matching_s matching
packet matching information
nm_core_poll_func_t p_poll_func
PUK_LIST_LINK(nm_core_pollable)
An internal tag.
nm_tag_t tag
the user-supplied tag
nm_session_hash_t hashcode
the session hashcode
asynchronous tasks for nmad core.
struct nm_req_s * p_unpack
struct nm_core_task_s::@7::@13 rtr_send
struct nm_core_task_s::@7::@9 completed_pw
struct nm_core_task_s::@7::@11 cancelled_prefetch
struct nm_core_task_s::@7::@14 handler
struct nm_core_task_s::@7::@12 pack_submission
void(* p_handler)(void)
enum nm_core_task_kind_e kind
union nm_core_task_s::@7 content
struct nm_core_task_s::@7::@8 unpack_next
struct nm_pkt_wrap_s * p_pw
struct nm_req_chunk_s * p_req_chunk
struct nm_core_task_s::@7::@10 completed_prefetch
struct nm_unexpected_s * p_unexpected
Core NewMadeleine structure.
Definition nm_core.h:43
block of static properties for a given data descriptor
Definition nm_data.h:94
a data descriptor, used to pack/unpack data from app layout to/from contiguous buffers
Definition nm_data.h:200
a driver.
Definition nm_drv.h:35
struct nm_core * p_core
Definition nm_drv.h:64
const char * url
driver url, as string
Definition nm_drv.h:55
const struct nm_minidriver_iface_s * driver
Driver interface, for use when no instance is needed.
Definition nm_drv.h:46
Connection to another process.
Definition nm_gate.h:109
status of tags on each gate
Definition nm_gate.h:27
containers for matching info, used for caching
struct nm_gtag_s * p_gtag
cache of gtag
struct nm_matching_gsession_s * p_gsession
cache of matching gsession
struct nm_matching_wildcard_s * p_wildcard
cache of matching wildcard
struct nm_matching_tag_s * p_matching_tag
cache of matching tag
struct to store matching info for any-source requests of a given tag
Definition nm_tags.h:146
struct to store matching info for wildcard requests, one per session
Definition nm_tags.h:126
generic monitor, used for requests and for global events (with matching)
nm_status_t event_mask
mask applied to status to check whether to fire events
nm_core_event_notifier_t p_notifier
notification function called to fire events
void * ref
opaque user-supplied pointer passed to notifier
Internal packet wrapper.
a chunk of request
PUK_LIST_LINK(nm_req_chunk)
nm_len_t chunk_offset
offset of the chunk relative to the full data in the req
struct nm_core_task_s core_task
nm_proto_t proto_flags
pre-computed proto flags
struct nm_data_properties_s chunk_props
properties of the data chunk
nm_len_t chunk_len
length of the chunk
struct nm_req_s * p_req
link to insert the req chunk as a core task
a generic pack/unpack request
nm_core_tag_t tag
tag to send to/from (works in combination with tag_mask for recv)
struct nm_req_s::@15::@18 unpack
struct nm_req_s::@15::@17 pack
nm_len_t expected_len
length of posted recv (may be updated if matched packet is shorter)
PUK_LIST_LINK(nm_req)
link to enqueue req in pending requests lists
nm_gate_t p_gate
dest/src gate; NULL if recv from any source
struct nm_matching_container_s matching
link to store request in a matching map
nm_len_t done
cumulated length of data sent so far
nm_prio_t priority
request priority level
uint32_t checksum
data checkusm when pack was submitted- for debug only
struct nm_req_pchunk_s * p_next
struct nm_gtag_s * p_gtag
cache for tag status on gate; NULL if tag or gate is unspecified yet
nm_len_t offset
offset of data partially received
struct nm_req_s::@15::@18::@19::nm_req_pchunk_s * p_pchunks
unsorted list of arrived chunks; reads are lock-free, writes are within core_core_lock sections
nm_cond_status_t status
status, including status bits and synchronization
nm_req_seq_t req_seq
request sequence number used to interleave wildcard/non-wildcard requests
nm_len_t chunk_offset
struct nm_req_chunk_s req_chunk
preallocated chunk for the common case (single-chunk)
nm_req_flag_t flags
flags given by user
struct nm_data_s data
data descriptor to send/recv
struct nm_pkt_wrap_s * p_prefetch_pw
packet wrapper to prefetch recv
nm_core_tag_t tag_mask
mask applied to tag for matching (only bits in mask need to match)
nm_len_t cumulated_len
amount of data unpacked so far
struct nm_req_s::@15::@18::@19 partition
partitioned unpack, used only if NM_REQ_FLAG_UNPACK_PARTITIONED is set
nm_len_t len
cumulated data length
int err
error status of the request
struct nm_monitor_s monitor
monitor attached to this request (only 1)
nm_seq_t seq
packet sequence number on the given tag
nm_len_t chunk_len
nm_len_t hlen
length of header to send eagerly
a track on a given gate
Definition nm_gate.h:69
a chunk of unexpected message to be stored
uuid in plain text (hex encoding)
Definition nm_types.h:124