-
Notifications
You must be signed in to change notification settings - Fork 26
/
stpool.h
1513 lines (1367 loc) · 45.5 KB
/
stpool.h
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
#ifndef __ST_POOL_H__
#define __ST_POOL_H__
/** @mainpage COPYRIGHT (C) 2014 - 2020, piggy_xrh
*
* Stpool is a portable and efficient tasks pool library, it can work on diferent
* platforms such as Windows, linux, unix and ARM.
*
* If you have any troubles or questions on using the pool, contact me.
*
* (Email: [email protected] QQ: 1169732280)
*/
#include <time.h>
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#ifdef _USRDLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
#else
#define EXPORT
#endif
#include "stpool_caps.h"
typedef struct cpool stpool_t;
/** Error code sets */
enum
{
/**
* System is out of memeory
*/
POOL_ERR_NOMEM = 1,
/**
* Task pool is being destroyed
*
* It indicates that @ref stpool_release has been called by user and
* the reference of the pool is zero
*/
POOL_ERR_DESTROYING = 2,
/**
* The throttle of the pool is turned on
*
* It indicates that user has called @ref stpool_throttle_enable (pool, 0)
* to turn the throttle swither on
*/
POOL_ERR_THROTTLE = 4,
/**
* The operation can not be done since the task is in progress now
*/
POOL_TASK_ERR_BUSY = 7,
/**
* The task has been marked with TASK_VMARK_DISABLE_QUEUE
*
* @see <pre>
* @ref stpool_task_mark
* @ref stpool_mark_all
* @ref stpool_mark_cb
* @ref stpool_group_mark_all
* @ref stpool_group_mark_cb
</pre>
*/
POOL_TASK_ERR_DISABLE_QUEUE = 8,
/**
* The task has not called @ref stpool_task_set_p to set its
* destination pool
*/
POOL_TASK_ERR_DESTINATION = 9,
/*----------------------------------------------------------------*/
/*------------------------- group error code --------------------*/
/*----------------------------------------------------------------*/
/**
* The group's throttle has been turned on by @ref stpool_group_throttle_enable
*/
POOL_ERR_GROUP_THROTTLE = 10,
/**
* The group does not exist
*/
POOL_ERR_GROUP_NOT_FOUND = 11,
/**
* The group is being destroyed
*/
POOL_ERR_GROUP_DESTROYING = 12,
/**
* The group is overloaded
*/
POOL_ERR_GROUP_OVERLOADED = 19,
/*----------------------------------------------------------------*/
/**
* The operation fails for its timeout
*/
POOL_ERR_TIMEDOUT = 13,
/**
* The wait function is waked up by user for some reasons
*
* @see @ref stpool_wakeid \n
* @ref stpool_wakeup
*/
POOL_ERR_INTERRUPTED = 14,
/**
* All other unkown errors
*
* It should not be changed in the future
*/
POOL_ERR_OTHER = 15,
/**
* The operation is not supported
*/
POOL_ERR_NSUPPORT = 16,
/**
* An error occured by the system library
*
* (The errno has been set properly)
*/
POOL_ERR_errno = 17,
/**
* The task can not be delived into the pool since the pool
* is overloaded now
*/
POOL_ERR_OVERLOADED = 18,
};
/** Task error reasons */
enum
{
/**
* The task is removed from the pool for some reasons
*/
eReason_removed = 0x01,
/**
* The task can not been executed since the pool has been marked
* suspended and the pool itself is being destroyed
*/
eReason_pool_destroying = 0x02,
/**
* The task can not been executed since the group is being destroyed
*/
eReason_group_destroying = 0x04,
/**
* The task can not been executed because of the pool's overload
*/
eReason_pool_overloaded = 0x08,
/**
* The task can not been executed because of the group's overload
*/
eReason_group_overloaded = 0x10
};
/** The definition of the task object */
struct sttask
{
/**
* A const string to describle the task
*/
const char *task_name;
/**
* The working routine of the task
*
* \@task_run will be called when the task is scheduled by the pool.
* user can do their customed works in this function.
*/
void (*task_run)(struct sttask *ptask);
/**
* The exception handler of the task
*
* If \@task_err_handler is not NULL, it will be called when one of the conditions
* below matches.
*
* . The task is removed from the pool for some reasons.
*/
void (*task_err_handler)(struct sttask *ptask, long reasons);
/**
* The argument reserved for task
*
* (The library will not touch it)
*/
void *task_arg;
/**
* The task code reserved for task
*
* (The library will not touch it)
*/
int task_code;
};
/** The policy to schedule the taskA */
enum
{
/**
* If there are tasks who has the same priority with taskA,
* the taskA will be scheduled prior to all of them
*/
ep_SCHE_TOP = 1,
/**
* If there are tasks who has the same priority with taskA,
* they will be scheduled prior to taskA
*/
ep_SCHE_BACK,
};
/** The scheduling attributes of the task object */
struct schattr
{
/**
* If \@permanent is not zero, the task's priority will not
* be changed until the user call @ref stpool_task_setschattr
* to modifiy it manually, or it will be set to zero after
* the next time to add it into the pool
*/
int permanent;
/**
* The priority of the task [0-99]
*/
int sche_pri;
/**
* The policy to schedule the task (ep_SCHE_TOP(BACK))
*/
int sche_pri_policy;
};
/** The status of the task object */
enum
{
/**
* Task is in the pending queue
*/
TASK_STAT_WAIT = (short)0x01,
/**
* Task is being scheduled by the pool
*/
TASK_STAT_SCHEDULING = (short)0x02,
/**
* Task has been marked with removed
*/
TASK_STAT_DISPATCHING = (short)0x08,
/**
* The task will be added into the pool automatically
* after its done
*/
TASK_STAT_WAIT_PENDING = (short)0x10,
};
/** VM Flags of the task object
*
* @note If a task has been really marked by the VM flags, they will
* be stored in the task object. and user can get the tasks' VM flags
* by @ref stpool_task_vm
*/
enum
{
/**
* Remove the task and the pool is responsible for calling its
* error handler in the background if it is not NULL.
*/
TASK_VMARK_REMOVE_BYPOOL = 0x0004,
/**
* Remove the task and the API itself is responsible for calling
* its error handler direcly if it is not NULL.
*/
TASK_VMARK_REMOVE = 0x0008,
/**
* The task can be added into the pool
*
* @note It is the default value
*/
TASK_VMARK_ENABLE_QUEUE = 0x0080,
/**
* The task is disallowed to be added into the pool
*/
TASK_VMARK_DISABLE_QUEUE = 0x0040,
/**
* The REMOVE mask sets:
* (TASK_VMARK_REMOVE_BYPOOL|TASK_VMARK_REMOVE)
*
* @note the library will ensure that the REMOVE flags will not be cleared until
* the user calls the APIs such as @ref stpool_task_queue, @ref stpool_add_routine,
* @ref stpool_clone_add_queue, and @ref stpool_group_add_routine to try to deliver
* the task into the pool's pending queue again.
*
*
* The mask sets that can be used by users to mark the tasks:
* (TASK_VMARK_REMOVE_FLAGS|TASK_VMARK_ENABLE_QUEUE|TASK_VMARK_DISABLE_QUEUE)
*
* @note User can get the recent VM flags of the task by @ref stpool_task_vm
*/
};
/** Schedule policy for the servering threads */
enum ep_SCHE
{
/**
* Default
*
* Do not change anything, use the OS default policy
*/
ep_SCHE_NONE,
/**
* Posix SCHED_RR
*/
ep_SCHE_RR,
/**
* Posix SCHED_FIFO
*/
ep_SCHE_FIFO,
/**
* Posix SCHED_OTHER
*/
ep_SCHE_OTHER
};
/** Attribute of servering thread */
struct stpool_thattr
{
/**
* Stack size (0:default)
*/
int stack_size;
/**
* Schedule policy
*/
enum ep_SCHE ep_schep;
/**
* schedule priority ([0-99) 0:default)
*/
int sche_priority;
};
/** Scheduling attributes of the thread */
struct stpool_taskattr
{
/**
* The max scheduling tasks of the thread's local queue
*/
int max_qscheduling;
/**
* The max dispatching tasks of the thread's local queue
*/
int max_qdispatching;
};
/** The overload actions */
enum
{
/**
* The newer task will be delived into the pending queue (default policy)
*/
eOA_none,
/**
* The newer task can not be delived into the pool
*/
eOA_discard,
/**
* The newer task will be delived into the pending queue, and menwhile the
* the oldest tasks existing in the pending queue will be removed
*/
eOA_drain
};
/** The overload policies */
struct oaattr
{
/**
* The threshold of the task existing in the pending queue
*/
int task_threshold;
/**
* The action that the library should execute when the task number arrives
* at the threshold
*/
int eoa;
};
/** Status of the pool */
struct pool_stat
{
/**
* A const string to describle the pool
*/
const char *desc;
/**
* The time when the pool is created
*/
time_t created;
/**
* The current user refereces
*
* @see @ref stpool_addref, @ref stpool_release
*/
long ref;
/**
* The number of waiters who is waiting on our pool
*/
int waiters;
/**
* The number of the priority queue passed to @ref stpool_create
*/
int priq_num;
/**
* A var indicates whether the pool's throttle switcher is on or not
*
* If it is none zero, it indicates that the throttle has been turned
* on, the pool will enject all requests delived by these APIs below:
*
* @ref stpool_task_queue
* @ref stpool_add_routine
* @ref stpool_group_add_routine.
*
* user can set the throttle status by @ref stpool_throttle_enable
*/
int throttle_enabled;
/**
* A var indicates whether the pool is active now or not
*
* If it is none zero, it indicates that the pool is inactive now,
* the pool will stop scheduling the pending tasks.
*
* @see @ref stpool_suspend, @ref stpool_resume
*/
int suspended;
/**
* The max limited servering threads number of the pool
*
* @see @ref stpool_adjust, @stpool_adjust_abs
*/
int maxthreads;
/**
* The reserved servering threads number of the pool
*
* If it is postive, the pool will make sure that there
* are \@minthreads created threads staying in the pool
* all the time.
*
* @see @ref stpool_adjust, @ref stpool_adjust_abs
*/
int minthreads;
/**
* The number of threads existing in the pool currently
*/
int curthreads;
/**
* The number of threads who is scheduling the tasks currently
*/
int curthreads_active;
/**
* The number of threads who is marked died currently
*
* @note the threads will be marked died if there are none any
* tasks existing in the pool to execute for a long time. the
* APIs @ref stpool_adjust, @ref stpool_adjust_abs, and @ref
* stpool_flush may also makr a few threads died.
*/
int curthreads_dying;
/**
* The base sleeping time for the free threads
*
* @note the threads will quit themself if they have not gotten
* any tasks for \@acttimeo + random() % @randtimeo milliseconds
*/
long acttimeo;
/**
* The random sleeping time for the free threads
*
* @see @ref acttimeo for more details
*/
long randtimeo;
/**
* The peek of the tasks number since the pool was created (-1 = UNKOWN)
*/
unsigned int tasks_peak;
/**
* The peek of the threads number since the pool was created.
*/
unsigned int threads_peak;
/**
* The number of tasks that has been added into the pool since
* the pool is created (-1 = UNKOWN)
*/
unsigned int tasks_added;
/**
* The number of tasks that has been processed by the pool since
* the pool is created (-1 = UNKOWN)
*/
unsigned int tasks_processed;
/**
* The number of tasks that has been removed by the pool since
* the pool is created (-1 = UNKOWN)
*/
unsigned int tasks_removed;
/**
* The number of tasks existing in the pool's pending queue
*/
unsigned int curtasks_pending;
/**
* The number of tasks who is being scheduled or is being removed by the pool
*/
unsigned int curtasks_scheduling;
/**
* The number of tasks who has been marked removed
*/
unsigned int curtasks_removing;
};
/** The walk callback that is used to visit the tasks */
typedef long (*Walk_cb)(struct sttask *ptask, void *opaque);
#ifdef __cplusplus
extern "C" {
#endif
/*----------------APIs about the task --------------*/
/**
* Acquire the real size of the task object
*/
EXPORT size_t stpool_task_size();
/**
* Initialize the task object with the specified parameters
*
* @param [in] ptask the task object needed to be initialized
* @param [in] pool the destination pool (Can be NULL)
* @param [in] task_name a const string to describle the task (NOTE: The library just copys its address)
* @param [in] task_run the task's working routine (Can not be NULL)
* @param [in] task_err_handler the task's completion routine (Can be NULL)
* @param [in] task_arg the argument reserved for task (Can be NULL)
*
* @return None
*
* @code example:
* struct sttask *ptask;
*
* ptask = malloc(stpool_task_size());
* stpool_task_init(ptask, pool, "task", run, complete, arg);
* ...
* @endcode
*/
EXPORT int stpool_task_init(struct sttask *ptask, stpool_t *pool,
const char *task_name, void (*task_run)(struct sttask *ptask),
void (*task_err_handler)(struct sttask *ptask, long reasons),
void *task_arg);
/**
* Allocate a task object from the global cache.
*
* @param @see @ref stpool_task_init
*
* @return On success, a task object, NULL otherwise
*
* @note The task returned by @ref stpool_task_new should be free manually
* by calling @ref stpool_task_delete
*/
EXPORT struct sttask *stpool_task_new(stpool_t *pool, const char *task_name,
void (*task_run)(struct sttask *ptask),
void (*task_err_handler)(struct sttask *ptask, long reasons),
void *task_arg);
/**
* Clone a task from the source task object.
*
* @param [in] ptask The source object.
* @param [in] clone_schattr Tell the system whether it should clone the task's scheduling attribute or not.
*
* @return a task object On success, NULL otherwise
*
* @note the cloned task should be free manually by calling @ref stpool_task_delete.
*/
EXPORT struct sttask *stpool_task_clone(struct sttask *ptask, int clone_schattr);
/**
* Destroy the task object that allocated by @ref stpool_task_new or @ref stpool_task_clone
*
* @param [in] ptask The task object that the user wants to destroy.
*
* @return none
*
* @note
* Only free tasks can be destroyed safely !
*
* But how can we detect whether the task is free now or not ? if one of
* the conditions listed below matches, it indicates that the task is free
* now.
* <pre>
* . @ref stpool_task_is_free returns a non-zero value
* . @ref stpool_task_wait (ptask, ms) returns 0
* . @ref stpool_task_stat (ptask) == 0
* . @ref stpool_task_stat2 (ptask, &vm) == 0
</pre>
* Normaly, the task will not be removed from the pool until the task's
* completion routine is executed completely. it means that it is not safe
* to destroy the task manually in the task's completion routine But If you
* really want to do this like that, you should call @ref stpool_task_detach
* firstly to remove the task from the pool completely.
*/
EXPORT void stpool_task_delete(struct sttask *ptask);
/*
* Set the destination pool for the task object
*
* @param [in] ptask the task object
* @param [in] pool the destination pool
*
* @return if the created pool does not support the customed tasks (see eCAP_F_CUSTOM_TASK),
* it returns POOL_ERR_NSUPPORT, or it returns 0
*
* @note the task should set its destination pool firstly before its being
* delived into the pool's pending queue by @ref stpool_task_queue,
* and if \@pool is not equal to the previous destination pool of
* the task, the flag TASK_VMAKR_DISABLE_QUEUE will be cleared by the
* library.
*/
EXPORT int stpool_task_set_p(struct sttask *ptask, stpool_t *pool);
/**
* Get the destination pool of the task
*
* @see @ref stpool_task_set_p
*/
EXPORT stpool_t *stpool_task_p(struct sttask *ptask);
/**
* Get the name of the pool that the task belongs to
*/
EXPORT const char *stpool_task_pname(struct sttask *ptask);
/**
* Set the task's user flags. (its range is from 0 to 0xffff)
*
* @param [in] ptask The task object.
* @param [in] uflags The private user flags that the user want to set
*
* @return None
*
* @note The user flags is reserved for users, the library will not touch it
*/
EXPORT void stpool_task_set_userflags(struct sttask *ptask, unsigned short uflags);
/**
* Get the task's user flags
*/
EXPORT unsigned short stpool_task_get_userflags(struct sttask *ptask);
/**
* Set the scheduling attribute for the task
*
* @param [in] ptask The task object.
* @param [in] attr The scheduling attribute that will be applyed on the task
*
* @return None
*/
EXPORT void stpool_task_setschattr(struct sttask *ptask, struct schattr *attr);
/**
* Get the scheduling attribute of the task
*
* @param [in] ptask The task object.
* @param [out] attr The buffer that used to received the task's scheduling attribute
*
* @return None
*/
EXPORT void stpool_task_getschattr(struct sttask *ptask, struct schattr *attr);
/**
* Get the status of the task
*
* @param [in] ptask the task object
*
* @return the status of the task
*/
EXPORT long stpool_task_stat(struct sttask *ptask);
/**
* Get the mark(VM) flags of the task
*
* @param [in] ptask the task object
*
* @return the recent VM flags of the task
*/
EXPORT long stpool_task_vm(struct sttask *ptask);
/**
* Get the mark(VM) flags and status of the task
*
* @param [in] ptask the task object
* @param [in, out] the buffer to receive the mark flags
*
* @return the status of the task
*/
EXPORT long stpool_task_stat2(struct sttask *ptask, long *vm);
/**
* Deliver the task into the pending queue of the destination pool
*
* @param [in] the task object
*
* @return 0 on success, error code listed below on error
* \n
* @ref POOL_ERR_DESTROYING \n
* @ref POOL_ERR_THROTTLE \n
* @ref POOL_TASK_ERR_DESTINATION \n
* @ref POOL_TASK_ERR_DISABLE_QUEUE \n
* @ref POOL_ERR_GROUP_THROTTLE \n
* @ref POOL_ERR_GROUP_DESTROYING \n
* @ref POOL_ERR_NSUPPORT \n
*
* @note
* @ref stpool_task_queue can be called one more times, If the task
* is already in the pool's pending queue, it does nothing, but if
* the task is being scheduled or is being removed, it will mark the
* task with @DO_AGAIN. and as a result, the task will be added into the
* pool again automatically after the pool's having executed the task's
* callback \n
* \n
* FAQ: If the task has been marked with @DO_AGAIN, what will it
* happen if the user mark the task with @ref TASK_VMARK_REMOVE_FLAGS again
* by calling APIs such as @ref stpool_mark_all, @ref stpool_task_mark, etc ? \n
* \n
* In this situations, the pool just remove the \@DO_AGAIN flag
* for the task. and the task will not be marked removed.
*
* @see @ref stpool_add_routine
*/
EXPORT int stpool_task_queue(struct sttask *ptask);
/**
* Remove the task
*
* @param [in] ptask the task object needed to be removed
* @param [in] dispatched_by_pool if it is none zero, this API will call @ref stpool_task_mark
* (ptask, TASK_VMARK_REMOVE_BYPOOL), or it will call @ref stpool_task_mark
* (ptask, TASK_VMARK_REMOVE)
*
* @return it returns 0 If the task is free now, or it returs 1.
*/
EXPORT int stpool_task_remove(struct sttask *ptask, int dispatched_by_pool);
/**
* Mark the task with the specified flags
*
* @param [in] ptask the task object that will be marked with \@lflags
* @param [in] lflags see @ref stpool_mark_all for more details about the mark flags
*
* @return None
*/
EXPORT void stpool_task_mark(struct sttask *ptask, long lflags);
/**
* Deatch a traceable task from the pool. the purpose to export this
* API is to allow users to destroy its task in the task's callbacks.
*
* @note
* @ref stpool_task_detach will be only allowed to be called in
* the task's working routine or in the task's error handler.
* Usally a traceable task will be connected with a pool after its
* having been added into the pool succefully. the task will be removed
* from the pool if its callback has been done completly, it means
* that it is not safe to destroy the task object in the task's callback,
* @stpool_task_detach is designed to resolve this problem, it will force
* the pool remove the task from its destination pool. and then the user
* can free the task object safely.
*
* @code example:
* #define MYTASK_AUTO_FREE 0x1
*
* void task_run(struct sttask *ptask) {
* // TO DO
*
* // Try to free the customed task in its working routine,
* // here we just call @task_err_handler to free it directly
* task_err_handler(ptask, 0);
* }
*
* void task_err_handler(struct sttask *ptask, long reasons) {
* // Try to free the customed task in its error handler
* if (MYTASK_AUTO_FREE & stpol_get_userflags(ptask)) {
* stpool_task_detach(ptask);
* stpool_delete_task(ptask);
* return;
* }
* }
*
* task = stpool_task_new(pool, "task", task_run, task_err_handler, task_arg);
* stpool_task_set_userflags(ptask, MYTASK_AUTO_FREE);
* stpool_task_queue(task);
*
* (Here we do not concern about when to free the customed task object, we just
* free it in its callback if we do not use it any more)
* @endcode
*
* @return None
*/
EXPORT void stpool_task_detach(struct sttask *ptask);
/**
* Check whether the task is free or not
*
* @return 0 if the task is not free
*
* @note its function is equals to (0 == @ref stpool_task_stat (ptask)),
* but it is much more faster.
*
* (Only free tasks can be destroyed safely)
*/
EXPORT int stpool_task_is_free(struct sttask *ptask);
/**
* Wait for the pool's throtle in \@ms milliseconds
*
* @param [in] ptask the task object
* @param [in] ms the timeout. (-1 = INFINITE)
*
* @return 0 on success, error code listed below otherwise.
* \n
* @ref POOL_ERR_TIMEDOUT \n
* @ref POOL_TASK_ERR_DESTINATION
* @ref POOL_ERR_DESTROYING
*
* @note If the destination pool or group doest not support the throttle,
* their throttles are always off.
*/
EXPORT int stpool_task_pthrottle_wait(struct sttask *ptask, long ms);
/**
* Wait for the task's completion in \@ms milliseconds
*
* @param [in] ptask the task object
* @param [in] ms the timeout. (-1 = INFINITE)
*
* @return 0 on success, error code listed below otherwise.
* \n
* @ref POOL_ERR_TIMEDOUT \n
* @ref POOL_TASK_ERR_DESTINATION \n
* @ref POOL_ERR_NSUPPORT \n
*/
EXPORT int stpool_task_wait(struct sttask *ptask, long ms);
/**
* Wait for the all tasks' completions in \@ms milliseconds
*
* @param [in] ptask the task object
* @param [in] entry the task objects array
* @param [in] n the length of the array
* @param [in] ms the timeout. (-1 = INFINITE)
*
* @return 0 on success, error code listed below otherwise.
* \n
* @ref POOL_TASK_ERR_DESTINATION \n
* @ref POOL_ERR_GROUP_NOT_FOUND \n
* @ref POOL_ERR_TIMEDOUT \n
* @ref POOL_ERR_NSUPPORT \n
*
* @note the NULL items will be skiped, and if the entry does not contain any items,
* This API returns 0
*
* @see @ref stpool_wait_all, @stpool_wait_cb
*/
EXPORT int stpool_task_wait_all(struct sttask *entry[], int n, long ms);
/**
* Wait for any tasks' completions in \@ms milliseconds
*
* @param [in] ptask the task object
* @param [in] entry the task objects array
* @param [in] n the length of the array
* @param [in] ms the timeout. (-1 = INFINITE)
*
* @return 0 on success, error code listed below otherwise.
* \n
* @ref POOL_TASK_ERR_DESTINATION \n
* @ref POOL_ERR_GROUP_NOT_FOUND \n
* @ref POOL_ERR_TIMEDOUT \n
* @ref POOL_ERR_NSUPPORT \n
*
* @note the NULL items will be skiped, and if the entry does not contain any items,
* This API returns 0
*/
EXPORT int stpool_task_wait_any(struct sttask *entry[], int n, long ms);
/*----------------APIs about the pool --------------*/
/**
* A const string to describle the version of the pool
*
* y/m/d-version-desc
*/
EXPORT const char *stpool_version();
/**
* Get the description of the error code
*
* @param [in] error the error code returned by the library
*
* @return the const string to describle the error code
*/
EXPORT const char *stpool_strerror(int error);
/**
* Create a task pool
*
* @param [in] desc a const string to describle the pool. (the library will copy its contents)
* @param [in] eCAPs the neccessary capabilities that the pool must support
* (see stpool_caps.h for more details)
*
* @param [in] maxthreads the limited number of working threads who will be
* created by pool to provide services.
*
* @param [in] minthreads the min number of working threads that should be
* reserved to wait for tasks. (the reserved threads
* will not quit even if there are none any tasks
* existing in the pool's pending queue)
*
* @param [in] suspend if \@suspend is 1, the pool will not schedule any
* tasks that have been added into the pool untill
* the user calls @ref stpool_resume to mark it active
*
* @param [in] pri_q_num the number of the priority queues that the user wants
* to create. a priority task will be inserted into a
* propriate priority queue by order according to its
* priority, and the tasks who has the higher priority
* will be scheduled prior to the tasks who has a lower
* priority.
* <pre>
* taskPendingQueue (priority queue)
* |
* |
* -------------------------------------------------------
* |task_0_top | ..... |task_k_top |
* |task_0_top | ..... |task_k_top |
* |task_0_back| ..... |task_k_back|
* |task_0_back| ..... |task_k_back|
* | ..... | ..... | .... |
* ------------- -------------
* queue[0] queue[n-1]
* (task_\@pri_\@sche_policy)
* (n = \@pri_q_num, 0 <= k <= 99)
* \n
* Each priority queue has m priorities (m = 100 / \@pri_q_num).