/usr/include/sphde/sphdirectpcqueue.h is in libsphde-dev 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* Copyright (c) 2015 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation, Steven Munroe - initial API and implementation
*/
#ifndef SRC_SPHDIRECTPCQUEUE_H_
#define SRC_SPHDIRECTPCQUEUE_H_
#include <stdint.h>
/** \file sphdirectpcqueue.h
* \brief Shared Persistent Heap, single producer single consumer queue direct API.
*
*
* For shared memory multi-thread/multi-core applications.
* This implementation is based on the Lock
* Free Producer/Consumer Queue (SPHSinglePCQueue_t) but
* simplifies access to the Entry for lower latency.
*
* This API supports atomic allocation of storage for queue
* entries for zero copy persistence and sharing. Zero copy queues
* divides the process of producing a queue entry in to three steps:
* - Allocate the queue entry (and initialize the header)
* - Use the return entry handle to fill in application specific data.
* - Marks the entry complete in the header.
*
* \code
#include <sphdirectpcqueue.h>
sphLFEntryID_t entry_tmp;
// only need to do this once per pcqueue and so should be
// outside of the primary producer message loop.
entry_tmp = SPHSinglePCQueueGetEntryTemplate(pqueue);
SPHLFEntryDirect_t handle;
int *array;
handle = SPHSinglePCQueueAllocStrideDirectSpin (pqueue);
if (handle)
{
array = (int *) SPHLFEntryDirectGetFreePtr (handle);
array[0] = val1;
array[1] = val2;
array[2] = val3;
SPHLFEntryDirectComplete (handle, entry_tmp, 1, 2);
}
else
{
// error handling
}
* \endcode
*
* The consumer can access queue entries once they are marked complete.
* The consumer:
* - checks (spins) for the next allocated entry to become complete.
* - uses the returned entry handle to directly access the entry contents.
* - When done processing the queue entry, it marks the entry header
* invalid and deallocates the entry.
* - This makes the next queue entry available, if any.
*
* \code
#include <sphdirectpcqueue.h>
int *array;
int data1, data2, data3;
handle = SPHSinglePCQueueGetNextCompleteDirectSpin (cqueue);
if (handle)
{
array = (int *) SPHLFEntryDirectGetFreePtr (handle);
data1 = array[0];
data2 = array[1];
data3 = array[2];
if (SPHSinglePCQueueFreeNextEntryDirect (cqueue, handle))
{
// complete handling of message
}
else
{ // error handling
printf ("SPHSinglePCQueueFreeNextEntry() = failed\n");
}
}
else
{ // error handling
printf ("SPHSinglePCQueueGetNextCompleteDirectSpin() = failed\n");
}
* \endcode
*
* In this implementation the allocation of the entry is minimally
* serialized based on the assumption that only one (producer) thread
* will be allocating queue entries.
* Likewise the assumption is that there is only one consumer thread
* per SPHSinglePCQueue_t instance.
* This allows independent producer/consumer thread pairs to interact
* with a queue instance with minimum synchronization and overhead.
*
* As an option the queue entry allocator will fill in a 4 byte
* entry header with:
* - Entry status and length.
* - Entry identifying Category and SubCategory codes.
*
* Any additional storage allocated to the entry (after the header)
* is available for application specific data. This API also provides
* a direct pointer mechanism to store application data.
* The API provides a completion function (SPHSinglePCQueueEntryComplete)
* which provides any memory barriers required by the platform and
* marks the entry complete.
*
* The API support simple circular queues and requires a constant
* entry stride. A stride that matches or is multiple of the
* cache line size can improve performance by avoiding
* "false sharing" of cache lines containing multiple queue entries
* across cores/sockets.
*
*/
#include "sastype.h"
#include "sasatom.h"
#include "sphlfentry.h"
#include "sphsinglepcqueue.h"
/** \brief ignore this macro behind the curtain **/
#ifdef __cplusplus
#define __C__ "C"
#else
#define __C__
#endif
/** \brief Instance of a Lock Free event direct data Handle.
*
* Contains fields required to: locate the entry, record the total
* space allocated to the entry, and manage the next location within
* the entry and remaining storage.
*
* Entry Handles should be allocated in private (local stack) storage
* to allow concurrent access to independent entries from multiple threads.
*/
typedef void* SPHLFEntryDirect_t;
/** \brief Marks the entry specified by the entry handle as complete.
* Also executes any memory barriers required by the platform to ensure
* that all previous stores by this thread to this entry are complete.
*
* @param directHandle Entry Handle for an allocated entry.
* @param entry_template from SPHSinglePCQueueGetEntryTemplate().
* @param catcode Category code to the completed entry.
* @param subcode Subcategory code to the completed entry.
* @return a 1 value indicates success.
*/
static inline int
SPHLFEntryDirectComplete (SPHLFEntryDirect_t directHandle,
sphLFEntryID_t entry_template,
int catcode, int subcode)
{
int rc = 1;
SPHLFEntryHeader_t *entryPtr = (SPHLFEntryHeader_t*)directHandle;
sphLFEntry_t entrytemp;
sas_read_barrier();
/* the template should have allocated and len already set. */
entrytemp.idUnit = entry_template;
entrytemp.detail.valid = 1;
entrytemp.detail.category = catcode;
entrytemp.detail.subcat = subcode;
entryPtr->entryID.idUnit = entrytemp.idUnit;
return (rc);
}
/** \brief Return the first free byte address for the direct entry
* specified by the direct entry handle. This is normally the byte
* after the sphLFEntry_t.
*
* \warning This function should be used carefully. It is may not
* provide the correct alignment for the data that follow and does not
* manage the space within the direct entry, if multiple application
* functions may update the same entry.
*
* @param directHandle Entry Handle for an allocated entry.
* @return address the entries free space.
*/
static inline void*
SPHLFEntryDirectGetFreePtr (SPHLFEntryDirect_t directHandle)
{
char *ptr = (char*)directHandle + sizeof (sphLFEntry_t);
return ((void*)ptr);
}
/** \brief Return the first free byte address, with required alignment,
* within the direct entry specified by the direct entry handle.
* This is normally the address after the sphLFEntry_t plus alignment
* padding.
*
* \warning This function does not manage the space within the direct
* entry, this may be an issue if multiple application functions
* update the same entry.
*
* @param directHandle Entry Handle for an allocated entry.
* @param alignval required alignment of the next value to be added.
* @return address the entries free space.
*/
static inline void*
SPHLFEntryDirectGetPtrAligned (SPHLFEntryDirect_t directHandle,
size_t alignval)
{
uintptr_t ptr = (uintptr_t)directHandle;
uintptr_t adjust = alignval - 1;
uintptr_t mask = ~adjust;
ptr = ((ptr + sizeof (sphLFEntry_t) + adjust) & mask);
return ((void*)ptr);
}
/** \brief Return the next free byte address within direct entry
* specified by a current address within that direct entry.
*
* \warning This function does not manage the space within the direct
* entry, this may be an issue if multiple application functions
* update the same entry.
*
* @param directptr current data address within Entry.
* @param incval size of last entry address to stepped over.
* @param alignval required alignment of the next value to be added.
* @return next address within the entry with require alignment.
*/
static inline void*
SPHLFEntryDirectIncAndAlign (void *directptr, size_t incval, size_t alignval)
{
uintptr_t ptr = (uintptr_t)directptr;
uintptr_t adjust = alignval - 1;
uintptr_t mask = ~adjust;
ptr = ((ptr + incval + adjust) & mask);
return ((void*)ptr);
}
/** \brief Return the status of the entry specified by the direct entry
* handle.
*
* @param directHandle Entry Handle for an allocated entry.
* @return true if the entry was complete (SPHLFLoggerEntryComplete
* has been called fo this entry). Otherwise False.
*/
static inline int
SPHLFEntryDirectIsComplete (SPHLFEntryDirect_t directHandle)
{
SPHLFEntryHeader_t *entryPtr = (SPHLFEntryHeader_t*)directHandle;
return (entryPtr->entryID.detail.valid == 1);
}
/** \brief Return the status of the entry specified by the direct entry
* handle.
*
* @param directHandle Entry Handle for an allocated entry.
* @return true if the entry was time stamped. Otherwise False.
*/
static inline int
SPHLFEntryDirectIsTimestamped (SPHLFEntryDirect_t directHandle)
{
SPHLFEntryHeader_t *entryPtr = (SPHLFEntryHeader_t*)directHandle;
return ((entryPtr->entryID.detail.valid == 1)
&&(entryPtr->entryID.detail.timestamped == 1));
}
/** \brief Return the entry category for the entry specified by the
* direct entry handle.
*
* @param directHandle Entry Handle for an allocated entry.
* @return the category from the entry, if the entry was valid.
* Otherwise return 0.
*/
static inline int
SPHLFEntryDirectCategory (SPHLFEntryDirect_t directHandle)
{
SPHLFEntryHeader_t *entryPtr = (SPHLFEntryHeader_t*)directHandle;
int result = -1;
#if 0
printf ("SPHLFEntryCategory(%p) entry=%p id=%x\n",
handlespace, entryPtr, entryPtr->entryID.idUnit);
#endif
if (entryPtr->entryID.detail.valid == 1)
result = entryPtr->entryID.detail.category;
return (result);
}
/** \brief Return the entry sub-category for the entry specified by the
* direct entry handle.
*
* @param directHandle Entry Handle for an allocated entry.
* @return the sub-category from the entry, if the entry was valid.
* Otherwise return 0.
*/
static inline int
SPHLFEntryDirectSubcat (SPHLFEntryDirect_t directHandle)
{
SPHLFEntryHeader_t *entryPtr = (SPHLFEntryHeader_t*)directHandle;
int result = -1;
#if 0
printf ("SPHLFEntrySubcat(%p) entry=%p id=%x\n",
handlespace, entryPtr, entryPtr->entryID.idUnit);
#endif
if (entryPtr->entryID.detail.valid == 1)
result = entryPtr->entryID.detail.subcat;
return (result);
}
/** \brief Return the entry template for an existing Lock Free
* Single Producer Single Consumer Queue.
* This template is used later to mark an allocated entry complete.
*
* @param queue Handle of a producer consumer queue.
* @return the entry template for this queue or
* 0 if not a valid SPHSinglePCQueue_t.
*/
extern __C__ sphLFEntryID_t
SPHSinglePCQueueGetEntryTemplate (SPHSinglePCQueue_t queue);
/** \brief Allows the producer thread to allocate and initialize the
* header of a queue entry for access. The allocation is from the
* specified Single Producer Single Consumer Queue.
*
* The allocation size is the stride set when the PC queue was
* initialized/created.
* The Entry status and length
* are stored in the header of the new entry.
* Returns an dire ctentry handle which allows the application to insert
* application specific data into the entry via the sphlfentry.h API.
* If the specified queue is full the allocation may fail.
*
* \note The queue entry is not ready for access by the Consumer
* thread, until additional application data is inserted and the
* entry is completed (via SPHLFEntryDirectComplete).
* Category and Subcategory may be supplied as the entry is completed.
*
* @param queue Handle of a producer consumer queue.
* @return Direct handle of the initialized queue entry,
* or 0 (NULL) if the allocation failed.
* For example the Allocate may fail if the queue is full.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueAllocStrideDirect (SPHSinglePCQueue_t queue);
/** \brief Allows the producer thread to allocate and initialize the
* header of a queue entry for access. The allocation is from the
* specified Single Producer Single Consumer Queue. If space is not
* Immediately available, spin until it is.
*
* The allocation size is the stride set when the PC queue was
* initialized/created.
* The Entry status and length
* are stored in the header of the new entry.
* Returns an dire ctentry handle which allows the application to insert
* application specific data into the entry via the sphlfentry.h API.
* If the specified queue is full the allocation may fail.
*
* \note The queue entry is not ready for access by the Consumer
* thread, until additional application data is inserted and the
* entry is completed (via SPHLFEntryDirectComplete).
* Category and Subcategory may be supplied as the entry is completed.
*
* @param queue Handle of a producer consumer queue.
* @return Direct handle of the initialized queue entry,
* or 0 (NULL) if the allocation failed.
* For example the Allocate may fail if the queue is full.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueAllocStrideDirectSpin (SPHSinglePCQueue_t queue);
/** \brief Allows the producer thread to allocate and initialize the
* header of a queue entry for access. The allocation is from the
* specified Single Producer Single Consumer Queue. If space is not
* Immediately available, spin until it is.
* While spinning use appropriate arch specific instructions to free
* up core resources for other threads.
*
* The allocation size is the stride set when the PC queue was
* initialized/created.
* The Entry status and length
* are stored in the header of the new entry.
* Returns an dire ctentry handle which allows the application to insert
* application specific data into the entry via the sphlfentry.h API.
* If the specified queue is full the allocation may fail.
*
* \note The queue entry is not ready for access by the Consumer
* thread, until additional application data is inserted and the
* entry is completed (via SPHLFEntryDirectComplete).
* Category and Subcategory may be supplied as the entry is completed.
*
* @param queue Handle of a producer consumer queue.
* @return Direct handle of the initialized queue entry,
* or 0 (NULL) if the allocation failed.
* For example the Allocate may fail if the queue is full.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueAllocStrideDirectSpinPause (SPHSinglePCQueue_t queue);
/** \brief Allows the consumer to get the next completed queue entry
* from the specified single producer single consumer queue.
*
* Returns an direct entry handle which allows the application
* to access the application specific data inserted by the
* produced thread.
* If the specified queue is empty or the next queue is not yet
* completed, spin until data is ready.
*
* @param queue Handle of a producer consumer queue.
* @return Direct Handle of the initialized logger entry,
* or 0 (NULL) if the get failed.
* For example the Get id the queue is not actually a
* SPHSinglePCQueue_t.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueGetNextCompleteDirectSpin (SPHSinglePCQueue_t queue);
/** \brief Allows the consumer to get the next completed queue entry
* from the specified single producer single consumer queue.
*
* Returns an direct entry handle which allows the application
* to access the application specific data inserted by the
* produced thread.
* If the specified queue is empty or the next queue is not yet
* completed, spin until data is ready.
* While spinning use appropriate arch specific instructions to
* free up core resources for other threads.
*
* @param queue Handle of a producer consumer queue.
* @return Direct Handle of the initialized logger entry,
* or 0 (NULL) if the get failed.
* For example the Get id the queue is not actually a
* SPHSinglePCQueue_t.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueGetNextCompleteDirectSpinPause (SPHSinglePCQueue_t queue);
/** \brief Allows the consumer to get the next completed queue entry
* from the specified single producer single consumer queue.
*
* Returns an direct entry handle which allows the application
* to access the application specific data inserted by the
* produced thread.
* If the specified queue is empty or the next queue is not yet
* completed the get may fail.
*
* @param queue Handle of a producer consumer queue.
* @return Direct Handle of the initialized logger entry,
* or 0 (NULL) if the get failed.
* For example the Get may fail if the queue
* is empty or the next tail entry is not yet completed.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueGetNextCompleteDirect (SPHSinglePCQueue_t queue);
/** \brief Allows the consumer to free the queue entry it just processed
* (using SPHSinglePCQueueGetNextComplete),
* from the specified single producer single consumer queue.
*
* Mark the current queue tail entry as free (unallocated and invalid)
* and bump the queue tail pointer to the next entry.
* If the specified queue is empty or the current tail entry is not yet
* completed the Free may fail.
*
* \warning The Consumer thread should not touch or modify a queue entry
* after calling FreeEntry.
* This is important to both correctness and performance.
*
* @param queue Handle of a producer consumer queue,
* @param next_entry Direct handle of the queue entry to free.
* @return True for successful tail free, otherwise indicated failure.
* For example the Free may fail if the queue
* is empty or the next tail entry is not yet completed.
*/
extern __C__ int
SPHSinglePCQueueFreeNextEntryDirect (SPHSinglePCQueue_t queue,
SPHLFEntryDirect_t next_entry);
/** \brief Allows the consumer to get the next allocated queue entry
* from the specified single producer single consumer queue.
*
* Returns an direct entry handle which allows the application to
* access the entry allocated by the produced thread.
* If the specified queue is empty or the next queue is not yet
* allocated the get may fail.
* Returning a entry does not mean the the producer has completed
* the entry and the consumer wait/spin (SPHLFEntryDirectIsComplete)
* for the entry to become complete.
*
* @param queue Handle of a producer consumer queue.
* @return Direct Handle of the initialized logger entry,
* or 0 (NULL) if the get failed.
* For example the Get may fail if the queue
* is empty or the next tail entry is not yet allocated.
*/
extern __C__ SPHLFEntryDirect_t
SPHSinglePCQueueGetNextEntryDirect (SPHSinglePCQueue_t queue);
/** \brief Return the status of the entry specified by the direct entry handle.
*
* @param directHandle entry Handle for an allocated entry.
* @return true if the entry was complete (SPHSinglePCQueueEntryComplete
* has been called for this entry). Otherwise False.
*/
extern __C__ int
SPHSinglePCQueueEntryIsCompleteDirect (SPHLFEntryDirect_t directHandle);
#endif /* SRC_SPHDIRECTPCQUEUE_H_ */
|