Changeset c1205bd for lib


Ignore:
Timestamp:
03/09/18 16:46:26 (3 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
cachetimestamps, develop, etsilive, master, rc-4.0.4, ringdecrementfix, ringperformance
Children:
7c33187
Parents:
62ee4ec
Message:

Add packet reference counting API

This API allows users to keep track of references to libtrace
packet structures that are not being released by the per packet
processing threads, e.g. those passed to the reporter or to
any user-created pthreads.

If you only have one reference to a packet at any given time,
you won't need this API. However, if you have multiple
concurrent references, this API will allow you to ensure that the
packet is correctly freed only once all references have been
finished with.

Location:
lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • lib/libtrace.h.in

    r29cafc0 rc1205bd  
    560560        uint64_t internalid;            /** Internal identifier for the pkt */
    561561        void *srcbucket;
     562
     563        pthread_mutex_t ref_lock;       /**< Lock for reference counter */
     564        int refcount;                 /**< Reference counter */
     565
    562566} libtrace_packet_t;
    563567
     
    17751779DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
    17761780
    1777 
    17781781/** Read the next packet from an input trace
    17791782 *
  • lib/libtrace_parallel.h

    r5c07bfe rc1205bd  
    12721272DLLEXPORT void trace_free_packet(libtrace_t * libtrace, libtrace_packet_t * packet);
    12731273
     1274/** Increments the internal reference counter for a packet.
     1275 * @param packet        The packet opaque pointer
     1276 *
     1277 * You may wish to use this function (and its decrementing counterpart)
     1278 * in situations where you are retaining multiple references to a packet
     1279 * outside of the core packet processing function. This will ensure that
     1280 * the packet is not released until there are no more outstanding references
     1281 * to the packet anywhere in your program.
     1282 */
     1283DLLEXPORT void trace_increment_packet_refcount(libtrace_packet_t *packet);
     1284
     1285/** Decrements the internal reference counter for a packet.
     1286 * @param packet        The packet opaque pointer
     1287 *
     1288 * If the reference counter goes below one, trace_fin_packet() will be
     1289 * called on the packet.
     1290 *
     1291 * You may wish to use this function (and its incrementing counterpart)
     1292 * in situations where you are retaining multiple references to a packet
     1293 * outside of the core packet processing function. This will ensure that
     1294 * the packet is not released until there are no more outstanding references
     1295 * to the packet anywhere in your program.
     1296 */
     1297DLLEXPORT void trace_decrement_packet_refcount(libtrace_packet_t *packet);
     1298
     1299
    12741300/** Provides some basic information about a trace based on its input format.
    12751301 *
  • lib/trace.c

    re375e0f rc1205bd  
    805805
    806806        packet->buf_control=TRACE_CTRL_PACKET;
     807        pthread_mutex_init(&(packet->ref_lock), NULL);
    807808        trace_clear_cache(packet);
    808809        return packet;
     
    856857                free(packet->buffer);
    857858        }
     859        pthread_mutex_destroy(&(packet->ref_lock));
    858860        packet->buf_control=(buf_control_t)'\0';
    859861                                /* A "bad" value to force an assert
     
    23162318        packet->l3_remaining = 0;
    23172319        packet->l4_remaining = 0;
     2320        packet->refcount = 0;
    23182321
    23192322}
  • lib/trace_parallel.c

    re375e0f rc1205bd  
    26012601}
    26022602
     2603DLLEXPORT void trace_increment_packet_refcount(libtrace_packet_t *packet) {
     2604        pthread_mutex_lock(&(packet->ref_lock));
     2605        if (packet->refcount < 0) {
     2606                packet->refcount = 1;
     2607        } else {
     2608                packet->refcount ++;
     2609        }
     2610        pthread_mutex_unlock(&(packet->ref_lock));
     2611}
     2612
     2613DLLEXPORT void trace_decrement_packet_refcount(libtrace_packet_t *packet) {
     2614        pthread_mutex_lock(&(packet->ref_lock));
     2615        packet->refcount --;
     2616
     2617        if (packet->refcount <= 0) {
     2618                trace_free_packet(packet->trace, packet);
     2619        }
     2620        pthread_mutex_unlock(&(packet->ref_lock));
     2621}
     2622
     2623
    26032624DLLEXPORT libtrace_info_t *trace_get_information(libtrace_t * libtrace) {
    26042625        if (libtrace->format)
Note: See TracChangeset for help on using the changeset viewer.