mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
request_queue.c File Reference
#include <glib.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "g_logger.h"
#include "gen_tile.h"
#include "protocol.h"
#include "render_config.h"
#include "request_queue.h"

Functions

static int calcHashKey (struct request_queue *queue, struct item *item)
 Compute a hash bucket index for a metatile render request item.
static struct item * lookup_item_idx (struct request_queue *queue, struct item *item)
 Look up an existing queued or in-flight request matching (mx, my, z, xmlname).
static void insert_item_idx (struct request_queue *queue, struct item *item)
 Insert a request item into the queue's hash table index for O(1) duplicate lookup.
static void remove_item_idx (struct request_queue *queue, struct item *item)
 Remove a request item from the queue's hash table index.
static enum protoCmd pending (struct request_queue *queue, struct item *test)
 Check whether a matching metatile request is already queued or being rendered.
struct item * request_queue_fetch_request (struct request_queue *queue)
 Pop the highest-priority pending request from queue and move it to the active render list.
void request_queue_clear_requests_by_fd (struct request_queue *queue, int fd)
 Invalidate all queued/rendering references to a disconnected client socket fd.
enum protoCmd request_queue_add_request (struct request_queue *queue, struct item *item)
 Enqueue a new tile render request or coalesce it with an existing pending item.
void request_queue_remove_request (struct request_queue *queue, struct item *request, int render_time)
 Unlink a completed request from the active render list, remove it from the hash index, and record render timing statistics.
int request_queue_no_requests_queued (struct request_queue *queue, enum protoCmd priority)
 Query the current number of pending items in a specific priority sub-queue.
void request_queue_copy_stats (struct request_queue *queue, stats_struct *stats)
 Copy a thread-safe snapshot of the queue's cumulative render statistics.
struct request_queue * request_queue_init ()
 Allocate and initialize a new request_queue with empty circular queues and hash index.
void request_queue_close (struct request_queue *queue)
 Destroy the request queue mutex and free the hash index and queue structure.

Function Documentation

◆ calcHashKey()

int calcHashKey ( struct request_queue * queue,
struct item * item )
static

Compute a hash bucket index for a metatile render request item.

Combines a byte-sum hash of item->req.xmlname (9 bits), zoom level z (4 bits), metatile X coordinate mx (24 bits), and metatile Y coordinate my (24 bits) into a 64-bit integer and reduces it modulo queue->hashidxSize.

Parameters
queueRequest queue containing hashidxSize.
itemRender request item whose style name and (mx, my, z) are hashed.
Returns
Zero-based bucket index in [0, queue->hashidxSize - 1].

◆ insert_item_idx()

void insert_item_idx ( struct request_queue * queue,
struct item * item )
static

Insert a request item into the queue's hash table index for O(1) duplicate lookup.

Stores item directly in the inline head slot item_hashidx[key] if empty, or appends a heap-allocated struct item_idx node to the bucket's collision chain. Caller must hold queue->qLock.

Parameters
queueRequest queue owning the hash index.
itemRequest item to index.

◆ lookup_item_idx()

struct item * lookup_item_idx ( struct request_queue * queue,
struct item * item )
static

Look up an existing queued or in-flight request matching (mx, my, z, xmlname).

Caller must hold queue->qLock.

Parameters
queueRequest queue whose hash index will be searched.
itemCandidate request item to match against.
Returns
Pointer to the matching struct item already in the queue, or NULL if none exists.

◆ pending()

enum protoCmd pending ( struct request_queue * queue,
struct item * test )
static

Check whether a matching metatile request is already queued or being rendered.

If an identical request is actively rendering (queueRender) or waiting in an interactive queue (queueRequest, queueRequestPrio, queueRequestLow), attaches test to the existing item's duplicates list so all waiting clients receive a response when rendering completes, and returns cmdIgnore.

If an identical request is in queueDirty or queueRequestBulk (where clients do not block on completion), returns cmdNotDone so the caller immediately releases test. Caller must hold queue->qLock.

Parameters
queueRequest queue to inspect.
testNewly arrived request item to deduplicate.
Returns
cmdIgnore if coalesced as a duplicate, cmdNotDone if matched in dirty/bulk queues, or cmdRender if no matching request is currently pending.

◆ remove_item_idx()

void remove_item_idx ( struct request_queue * queue,
struct item * item )
static

Remove a request item from the queue's hash table index.

Handles both inline head-slot removal (promoting and freeing the first chained overflow node if present) and interior/tail chained node removal. Caller must hold queue->qLock.

Parameters
queueRequest queue owning the hash index.
itemRequest item to remove from the index.

◆ request_queue_add_request()

enum protoCmd request_queue_add_request ( struct request_queue * queue,
struct item * item )

Enqueue a new tile render request or coalesce it with an existing pending item.

Deduplicates item via pending(). If unique, places item onto the queue matching item->req.cmd (reqHead, reqPrioHead, reqLowHead, or reqBulkHead) when below REQ_LIMIT, or falls back to dirtyHead (with fd = FD_INVALID) when below DIRTY_LIMIT. Drops and frees item if both limits are saturated.

Parameters
queueRequest queue instance (must not be NULL).
itemHeap-allocated request item (ownership transferred to queue or freed on drop).
Returns
cmdIgnore if queued for synchronous client notification (or coalesced as a duplicate), or cmdNotDone if relegated to dirtyHead, dropped, or already dirty/bulk.

◆ request_queue_clear_requests_by_fd()

void request_queue_clear_requests_by_fd ( struct request_queue * queue,
int fd )

Invalidate all queued/rendering references to a disconnected client socket fd.

Walks the interactive and active render queues (reqHead, renderHead, reqPrioHead, reqBulkHead, reqLowHead) plus any attached duplicates chains, setting matching fd fields to FD_INVALID so worker threads do not attempt to write responses to a closed or reused descriptor. (dirtyHead items already hold FD_INVALID.)

Parameters
queueRequest queue instance.
fdClient socket file descriptor that was closed or errored.

◆ request_queue_close()

void request_queue_close ( struct request_queue * queue)

Destroy the request queue mutex and free the hash index and queue structure.

Parameters
queueRequest queue instance to tear down.

◆ request_queue_copy_stats()

void request_queue_copy_stats ( struct request_queue * queue,
stats_struct * stats )

Copy a thread-safe snapshot of the queue's cumulative render statistics.

Parameters
queueRequest queue instance.
statsDestination stats_struct buffer populated under queue->qLock.

◆ request_queue_fetch_request()

struct item * request_queue_fetch_request ( struct request_queue * queue)

Pop the highest-priority pending request from queue and move it to the active render list.

Blocks on queue->qCond until at least one pending queue is non-empty. Dequeues in strict priority order:

  1. reqPrioHead (cmdRenderPrio)
  2. reqHead (cmdRender)
  3. reqLowHead (cmdRenderLow)
  4. dirtyHead (cmdDirty)
  5. reqBulkHead (cmdRenderBulk)

Moves the selected item onto renderHead with inQueue = queueRender while keeping it registered in the hash index so incoming duplicate requests still coalesce with it.

Parameters
queueRequest queue instance.
Returns
Pointer to the dequeued struct item now marked queueRender.

◆ request_queue_init()

struct request_queue * request_queue_init ( )

Allocate and initialize a new request_queue with empty circular queues and hash index.

Initializes qLock, qCond, all six circular doubly-linked list sentinel heads (reqHead, reqPrioHead, reqLowHead, reqBulkHead, dirtyHead, renderHead), zeroed statistics counters, and the HASHIDX_SIZE-bucket hash table.

Returns
Newly allocated struct request_queue, or NULL if memory or pthread initialization fails.

◆ request_queue_no_requests_queued()

int request_queue_no_requests_queued ( struct request_queue * queue,
enum protoCmd priority )

Query the current number of pending items in a specific priority sub-queue.

Parameters
queueRequest queue instance.
priorityCommand enum identifying the sub-queue (cmdRenderPrio, cmdRender, cmdRenderLow, cmdDirty, or cmdRenderBulk).
Returns
Current item count in the requested sub-queue, or -1 if priority is unrecognized.

◆ request_queue_remove_request()

void request_queue_remove_request ( struct request_queue * queue,
struct item * request,
int render_time )

Unlink a completed request from the active render list, remove it from the hash index, and record render timing statistics.

Does not free request itself (the caller frees request and any chained duplicates after sending responses).

Parameters
queueRequest queue instance.
requestCompleted request item currently on renderHead.
render_timeElapsed render duration in milliseconds (recorded if > 0).