瀏覽代碼

cmThread.h/c: Added cmTs1p1cSetCallback().

master
kpl 11 年之前
父節點
當前提交
c1289070d2
共有 2 個文件被更改,包括 11 次插入226 次删除
  1. 8
    226
      cmThread.c
  2. 3
    0
      cmThread.h

+ 8
- 226
cmThread.c 查看文件

@@ -825,233 +825,7 @@ bool cmTsQueueIsValid( cmTsQueueH_t h )
825 825
 //--------------------------------------------------------------------------------------------------
826 826
 //--------------------------------------------------------------------------------------------------
827 827
 //--------------------------------------------------------------------------------------------------
828
-#ifdef NOT_DEF
829
-enum { kThBufCnt=2 };
830 828
 
831
-typedef struct
832
-{
833
-  char*             buf;
834
-  volatile unsigned ii;
835
-  volatile unsigned oi;
836
-} cmThBuf_t;
837
-
838
-typedef struct
839
-{
840
-  cmErr_t           err;
841
-  cmThBuf_t         a[kThBufCnt];
842
-  volatile unsigned ibi;
843
-  unsigned          bn;
844
-  cmTsQueueCb_t     cbFunc;
845
-  void*             cbArg;
846
-} cmTs1p1c_t;
847
-
848
-cmTs1p1c_t* _cmTs1p1cHandleToPtr( cmTs1p1cH_t h )
849
-{
850
-  cmTs1p1c_t* p = (cmTs1p1c_t*)h.h;
851
-  assert( p != NULL );
852
-  return p;
853
-}
854
-
855
-cmThRC_t _cmTs1p1cDestroy( cmTs1p1c_t* p )
856
-{
857
-  unsigned i;
858
-  for(i=0; i<kThBufCnt; ++i)
859
-    cmMemFree(p->a[i].buf);
860
-
861
-  cmMemFree(p);
862
-
863
-  return kOkThRC;
864
-}
865
-
866
-cmThRC_t   cmTs1p1cCreate( cmTs1p1cH_t* hPtr, unsigned bufByteCnt, cmTsQueueCb_t cbFunc, void* cbArg, cmRpt_t* rpt )
867
-{
868
-  cmThRC_t rc;
869
-  if((rc = cmTs1p1cDestroy(hPtr)) != kOkThRC )
870
-    return rc;
871
-  
872
-  unsigned    i;
873
-  cmTs1p1c_t* p = cmMemAllocZ(cmTs1p1c_t,1);
874
-  cmErrSetup(&p->err,rpt,"TS 1p1c Queue");
875
-  for(i=0; i<kThBufCnt; ++i)
876
-  {
877
-    p->a[i].buf = cmMemAllocZ(char,bufByteCnt);
878
-    p->a[i].ii  = 0;
879
-    p->a[i].oi  = bufByteCnt;
880
-  }
881
-
882
-  p->ibi        = 0;
883
-  p->bn         = bufByteCnt;
884
-  p->cbFunc     = cbFunc;
885
-  p->cbArg = cbArg;
886
-
887
-  hPtr->h = p;
888
-
889
-  return rc;
890
-}
891
-
892
-cmThRC_t cmTs1p1cDestroy( cmTs1p1cH_t* hp )
893
-{
894
-  cmThRC_t rc = kOkThRC;
895
-
896
-  if( hp == NULL || cmTs1p1cIsValid(*hp)==false )
897
-    return kOkThRC;
898
-
899
-  cmTs1p1c_t* p = _cmTs1p1cHandleToPtr(*hp);
900
-  if(( rc = _cmTs1p1cDestroy(p)) != kOkThRC )
901
-    return rc;
902
-  
903
-  hp->h = NULL;
904
-  return rc;
905
-}
906
-
907
-cmThRC_t   cmTs1p1cEnqueueSegMsg( cmTs1p1cH_t h, const void* msgPtrArray[], unsigned msgByteCntArray[], unsigned arrayCnt )
908
-{
909
-  cmThRC_t    rc = kOkThRC;
910
-  unsigned    mn = 0;
911
-  unsigned    i;
912
-  cmTs1p1c_t* p  = _cmTs1p1cHandleToPtr(h);
913
-  cmThBuf_t*  ib = p->a + p->ibi;
914
-  
915
-
916
-  // get the total count of bytes for this msg
917
-  for(i=0; i<arrayCnt; ++i)
918
-    mn += msgByteCntArray[i];
919
-
920
-  unsigned dn = mn + sizeof(unsigned);
921
-
922
-  // if the message is too big for even an empty buffer
923
-  if( dn > p->bn )
924
-    return cmErrMsg(&p->err,kBufFullThRC,"A msg containing %i bytes will never be able to fit in a queue with an empty size of %i bytes.",dn,p->bn);
925
-
926
-  // if the msg won't fit in the current input buffer then try swapping buffers.
927
-  if( ib->ii + dn > p->bn )
928
-  {
929
-    // get the current output buffer
930
-    cmThBuf_t* ob = p->a + (p->ibi==0 ? 1 : 0);
931
-
932
-    // Empty buffers will be set such that:  oi==bn and ii==0.
933
-    //
934
-    // Note that setting ii to 0 in an output buffer is the last operation
935
-    // performed on an empty output buffer. ii==0 is therefore the 
936
-    // signal that an output buffer can be reused for input.
937
-
938
-    // if the output buffer is not empty - then an overflow occurred
939
-    if( ob->ii != 0 )
940
-      return cmErrMsg(&p->err,kBufFullThRC,"The msq queue cannot accept a %i byte msg into %i bytes.",dn, p->bn - ib->ii);
941
-
942
-    // setup the initial output location of the new output buffer
943
-    ib->oi = 0;    
944
-                    
945
-    // swap buffers 
946
-    p->ibi = (p->ibi + 1) % kThBufCnt; 
947
-
948
-    // get the new input buffer 
949
-    ib     = ob;                       
950
-  }
951
-  
952
-  // get a pointer to the base of the write location
953
-  char* dp = ib->buf + ib->ii;
954
-
955
-  // write the length of the message
956
-  *(unsigned*)dp = mn;
957
-  dp += sizeof(unsigned);
958
-
959
-  // write the body of the message
960
-  for(i=0; i<arrayCnt; ++i)
961
-  {
962
-    memcpy(dp,msgPtrArray[i],msgByteCntArray[i]);
963
-    dp += msgByteCntArray[i];
964
-  }
965
-
966
-  // this MUST be executed last - we'll use 'dp' in the calculation
967
-  // (even though ib->ii += dn would be more straight forward way 
968
-  // to accomplish the same thing) to prevent the optimizer from 
969
-  // moving the assignment prior to the for loop.
970
-  ib->ii += dp - (ib->buf + ib->ii);
971
-
972
-  return rc;
973
-}
974
-
975
-cmThRC_t   cmTs1p1cEnqueueMsg( cmTsQueueH_t  h, const void* dataPtr, unsigned byteCnt )
976
-{ return cmTs1p1cEnqueueSegMsg(h,&dataPtr,&byteCnt,1); }
977
-
978
-unsigned   cmTs1p1cAllocByteCount( cmTs1p1cH_t h )
979
-{
980
-  cmTs1p1c_t* p = _cmTs1p1cHandleToPtr(h);
981
-  return p->bn;
982
-}
983
-
984
-unsigned   cmTs1p1cAvailByteCount( cmTs1p1cH_t h )
985
-{
986
-  cmTs1p1c_t* p = _cmTs1p1cHandleToPtr(h);
987
-  return p->bn - p->a[ p->ibi ].ii;
988
-}
989
-cmThRC_t   cmTs1p1cDequeueMsg( cmTs1p1cH_t  h,  void* dataPtr, unsigned byteCnt )
990
-{
991
-  cmThRC_t    rc = kOkThRC;
992
-  cmTs1p1c_t* p  = _cmTs1p1cHandleToPtr(h);
993
-  cmThBuf_t*  ob = p->a + (p->ibi == 0 ? 1 : 0);
994
-
995
-  // empty buffers always are set to: oi==bn && ii==0
996
-  if( ob->oi >= ob->ii )
997
-    return kBufEmptyThRC;
998
-
999
-  // get the size of the msg
1000
-  unsigned mn = *(unsigned*)(ob->buf + ob->oi);
1001
-
1002
-  // increment the current output location to the msg body
1003
-  ob->oi += sizeof(unsigned);
1004
-
1005
-  // copy or send the msg 
1006
-  if( dataPtr != NULL )
1007
-  {
1008
-    if( byteCnt < mn )
1009
-      return cmErrMsg(&p->err,kBufTooSmallThRC,"The return buffer constains too few bytes (%i) to contain %i bytes.",byteCnt,mn);
1010
-
1011
-    memcpy(dataPtr, ob->buf + ob->oi, mn);    
1012
-  }
1013
-  else
1014
-  {
1015
-    p->cbFunc(p->cbArg, mn, ob->buf + ob->oi );
1016
-  }
1017
-
1018
-  ob->oi += mn;
1019
-  
1020
-  // if we are reading correctly ob->oi should land 
1021
-  // exactly on ob->ii when the buffer is empty
1022
-  assert( ob->oi <= ob->ii );
1023
-
1024
-  // if the buffer is empty
1025
-  if( ob->oi == ob->ii )
1026
-  {
1027
-    ob->oi = p->bn; // mark the buffer as empty
1028
-    ob->ii = 0;     //
1029
-  }
1030
-
1031
-  return rc;
1032
-}
1033
-
1034
-
1035
-unsigned cmTs1p1cDequeueMsgByteCount( cmTsQueueH_t h )
1036
-{
1037
-  cmTs1p1c_t* p  = _cmTs1p1cHandleToPtr(h);
1038
-  cmThBuf_t*  ob = p->a + (p->ibi == 0 ? 1 : 0);
1039
-
1040
-  // empty buffers always are set to: oi==bn && ii==0
1041
-  if( ob->oi >= ob->ii )
1042
-    return 0;
1043
-
1044
-  // get the size of the msg
1045
-  return  *(unsigned*)(ob->buf + ob->oi);
1046
-}
1047
-
1048
-
1049
-bool     cmTs1p1cMsgWaiting( cmTsQueueH_t h )
1050
-{  return cmTs1p1cDequeueMsgByteCount(h) > 0;  }
1051
-
1052
-bool cmTs1p1cIsValid( cmTs1p1cH_t h )
1053
-{ return h.h != NULL; }
1054
-#endif
1055 829
 
1056 830
 //--------------------------------------------------------------------------------------------------
1057 831
 //--------------------------------------------------------------------------------------------------
@@ -1110,6 +884,14 @@ cmThRC_t cmTs1p1cDestroy( cmTs1p1cH_t* hp )
1110 884
   return rc;
1111 885
 }
1112 886
 
887
+cmThRC_t cmTs1p1cSetCallback( cmTs1p1cH_t h, cmTsQueueCb_t cbFunc, void* cbArg )
888
+{
889
+  cmTs1p1c_t* p = _cmTs1p1cHandleToPtr(h);
890
+  p->cbFunc = cbFunc;
891
+  p->cbArg = cbArg;
892
+  return kOkThRC;
893
+}
894
+
1113 895
 cmThRC_t   cmTs1p1cEnqueueSegMsg( cmTs1p1cH_t h, const void* msgPtrArray[], unsigned msgByteCntArray[], unsigned arrayCnt )
1114 896
 {
1115 897
   cmThRC_t    rc = kOkThRC;

+ 3
- 0
cmThread.h 查看文件

@@ -200,8 +200,11 @@ extern "C" {
200 200
   extern cmTs1p1cH_t cmTs1p1cNullHandle;
201 201
 
202 202
   cmThRC_t   cmTs1p1cCreate(     cmTs1p1cH_t* hPtr, unsigned bufByteCnt, cmTsQueueCb_t cbFunc, void* cbArg, cmRpt_t* rpt );
203
+
203 204
   cmThRC_t   cmTs1p1cDestroy(    cmTs1p1cH_t* hPtr );
204 205
 
206
+  cmThRC_t   cmTs1p1cSetCallback( cmTs1p1cH_t h, cmTsQueueCb_t cbFunc, void* cbArg );
207
+
205 208
   cmThRC_t   cmTs1p1cEnqueueSegMsg( cmTs1p1cH_t h, const void* msgPtrArray[], unsigned msgByteCntArray[], unsigned arrayCnt );
206 209
 
207 210
   cmThRC_t   cmTs1p1cEnqueueMsg( cmTs1p1cH_t  h, const void* dataPtr, unsigned byteCnt );

Loading…
取消
儲存