1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2013 Marek Olšák <maraeo@gmail.com>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/**
24bf215546Sopenharmony_ci * @file
25bf215546Sopenharmony_ci * 1D integer range, capable of the union and intersection operations.
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci * It only maintains a single interval which is extended when the union is
28bf215546Sopenharmony_ci * done. This implementation is partially thread-safe (readers are not
29bf215546Sopenharmony_ci * protected by a lock).
30bf215546Sopenharmony_ci *
31bf215546Sopenharmony_ci * @author Marek Olšák
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#ifndef U_RANGE_H
35bf215546Sopenharmony_ci#define U_RANGE_H
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "os/os_thread.h"
38bf215546Sopenharmony_ci#include "pipe/p_state.h"
39bf215546Sopenharmony_ci#include "pipe/p_screen.h"
40bf215546Sopenharmony_ci#include "util/u_atomic.h"
41bf215546Sopenharmony_ci#include "util/u_math.h"
42bf215546Sopenharmony_ci#include "util/simple_mtx.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistruct util_range {
45bf215546Sopenharmony_ci   unsigned start; /* inclusive */
46bf215546Sopenharmony_ci   unsigned end; /* exclusive */
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   /* for the range to be consistent with multiple contexts: */
49bf215546Sopenharmony_ci   simple_mtx_t write_mutex;
50bf215546Sopenharmony_ci};
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic inline void
54bf215546Sopenharmony_ciutil_range_set_empty(struct util_range *range)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   range->start = ~0;
57bf215546Sopenharmony_ci   range->end = 0;
58bf215546Sopenharmony_ci}
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci/* This is like a union of two sets. */
61bf215546Sopenharmony_cistatic inline void
62bf215546Sopenharmony_ciutil_range_add(struct pipe_resource *resource, struct util_range *range,
63bf215546Sopenharmony_ci               unsigned start, unsigned end)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci   if (start < range->start || end > range->end) {
66bf215546Sopenharmony_ci      if (resource->flags & PIPE_RESOURCE_FLAG_SINGLE_THREAD_USE ||
67bf215546Sopenharmony_ci          p_atomic_read(&resource->screen->num_contexts) == 1) {
68bf215546Sopenharmony_ci         range->start = MIN2(start, range->start);
69bf215546Sopenharmony_ci         range->end = MAX2(end, range->end);
70bf215546Sopenharmony_ci      } else {
71bf215546Sopenharmony_ci         simple_mtx_lock(&range->write_mutex);
72bf215546Sopenharmony_ci         range->start = MIN2(start, range->start);
73bf215546Sopenharmony_ci         range->end = MAX2(end, range->end);
74bf215546Sopenharmony_ci         simple_mtx_unlock(&range->write_mutex);
75bf215546Sopenharmony_ci      }
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic inline boolean
80bf215546Sopenharmony_ciutil_ranges_intersect(const struct util_range *range,
81bf215546Sopenharmony_ci                      unsigned start, unsigned end)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci   return MAX2(start, range->start) < MIN2(end, range->end);
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci/* Init/deinit */
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic inline void
90bf215546Sopenharmony_ciutil_range_init(struct util_range *range)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   (void) simple_mtx_init(&range->write_mutex, mtx_plain);
93bf215546Sopenharmony_ci   util_range_set_empty(range);
94bf215546Sopenharmony_ci}
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cistatic inline void
97bf215546Sopenharmony_ciutil_range_destroy(struct util_range *range)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   simple_mtx_destroy(&range->write_mutex);
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci#endif
103